﻿'''' Last Modified on Dec 15, 2014 09.35 am.
Imports System.Data
Imports System.Data.OleDb
Imports ADOEntityKYC
Partial Class KYC_NonIndividualKYC
    Inherits Web.UI.BasePage

#Region " Variable Gobal "

    Dim cn As New OleDbConnection
    Dim clsDatabase As New clsDatabase
    Dim Sql As String
    Dim ds As DataSet
    Dim TypeKYC As Integer = 0
    Dim CustSeq As Integer = 0
    Dim KYCEntity As KYCEntities = New KYCEntities
    Dim clsSessionMenuRight As clsSessionMenuRight = New clsSessionMenuRight
    Dim strMsg As String = ""
    Dim clsDataTable As New clsLINQToDataTable
    Dim strCdd As Integer
    Dim strCddOld As Integer
    Dim clsMoney As clsMoney
    Dim condition As Boolean = True
    
    Dim convertDate As clsDateFormat
    Dim acc As String

    '''''' Prachak ''''''''''''''''''''''''''''''
    Dim dtAddedService As New DataTable
    Dim dtProduct As New DataTable
    Dim dtC_Cust_Category As New DataTable
    Dim dtIncome As New DataTable
    Dim dtSanc As New DataTable
    Dim dtPolitical As New DataTable
    Dim dtReason As New DataTable
    Dim dtBo As New DataTable
    Dim lvsSQL As String
    Dim clsValid As clsValidateData
    Dim indexGrdDAccBefore As Int32 = -1
    Dim indexGrdBoBefore As Int32 = -1
    Dim intBeforeItemIndex As Integer = -1
    Dim indexGrdDSourceBefore As Int32 = -1
    Dim indexGrdLAccBefore As Int32 = -1
    Dim indexGrdPSourceBefore As Int32 = -1
    Dim indexGrdPAccBefore As Int32 = -1

#End Region

#Region "Even Page"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        clsValid = New clsValidateData()
        Me.PanelDepositSactionC.Visible = False
        Me.btAddAcc.Enabled = True


        If (Session("PopAddedService") Is Nothing) Then
            Me.grdPRODUCT.DataSource = dtProduct
            grdPRODUCT.DataBind()
        End If

        If (Session("PopProduct") Is Nothing) Then
            Me.grdPRODUCT.DataSource = dtProduct
            grdPRODUCT.DataBind()
        End If

        If (Session("PopCorpCate") Is Nothing) Then
            grdCorp_Cate.DataSource = dtC_Cust_Category
            grdCorp_Cate.DataBind()
        End If
        Try
            TypeKYC = Request.QueryString("TypeKYC")
            CustSeq = Request.QueryString("CUSTSEQ")


            ''''offer Script''''
            lbCUSTSEQ.Text = Request.QueryString("CUSTSEQ")
            lbCUSTTYPE.Text = Request.QueryString("KYC")

            btAddBo.Attributes.Add("onClick", "OpenBo('add');")   ''''showModalDialog
            btEditBo.Attributes.Add("onClick", "OpenBo('edit');")   ''''showModalDialog
            ''''End offer Script''''


            acc = Request.QueryString("acc")

            ds = Session("ds")
            clsSessionMenuRight = Session("clsMenuRight")

            If Not TypeKYC = 4 Then
                btnCancel.Attributes.Add("onclick", "return confirm('" + String.Format(Resources.resMsgKYC.msgCancle) + "')")
                btnCDD.Attributes.Add("onclick", "return confirm('" + String.Format(Resources.resMsgKYC.msgBtnCDD) + "')")
            Else
                ' btnCDD.Visible = False
            End If

            If Not IsPostBack Then
                chkIsLogin()
                CreateControlMP()
                'EnableKYC()
                VisbleKYC()
                loadDataToDropDown()
                If Not CustSeq = 0 Then
                    Me.loadKYCAccount()
                    ''''Prachak '''''''''''''''''''''''''
                    Me.Bind_Data_togrid_Corp_Category()
                    Me.Bind_Data_togrid_grdPolitical()
                    Me.Bind_Data_togrid_Income_Source()
                    Me.Bind_Data_togrid_Product()
                    Me.Bind_Data_togrdAddedService()
                    Me.loadBo()

                    If (Request.QueryString("KYC") = 1) Then
                        loadDObj(grdDAcc)
                        loadDsource(grdDSource)
                    ElseIf (Request.QueryString("KYC") = 2) Then
                        loadDObj(grdLAcc)
                    ElseIf (Request.QueryString("KYC") = 3) Then
                        loadDObj(grdPAcc)
                        loadDsource(grdPSource)
                    End If


                    BindingDataCBS()
                    'strCdd = hdfCdd.Value
                    BindingDataRef()
                    If Request.QueryString("KYC") <> 5 Then
                        loadKYCAccount()
                    End If

                    'If Request.QueryString("KYC") = 1 Then
                    '    BindingDataKYCDeposit()
                    'ElseIf Request.QueryString("KYC") = 2 Then
                    '    BindingDataKYCLoan()
                    'ElseIf Request.QueryString("KYC") = 3 Then
                    '    BindingDataKYCPN()
                    '    '  PanelCBS.Enabled = True
                    'Else
                    '    BindingDataKYCDeposit()
                    '    BindingDataKYCLoan()
                    '    BindingDataKYCPN()
                    'End If

                    indexBizChanging2()
                    indexBizChanging3()

                    'If Request.QueryString("CDD") = "1" Then
                    '    lblRiskLevel.ForeColor = Drawing.Color.Green
                    'Else
                    '    If Request.QueryString("CDD") = "2" Then
                    '        lblRiskLevel.ForeColor = Drawing.Color.Orange
                    '    Else
                    '        lblRiskLevel.ForeColor = Drawing.Color.Red
                    '    End If
                    'End If
                Else
                    loadDataAddr("", "", "", 1)
                End If
            Else
                'strCdd = hdfCdd.Value
            End If

            If hdfCdd.Value = "" Then
                ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                       UpdatePanel1.GetType(), _
                                                       "clientScript", _
                                                       "alert('" + "กรุณาประเมินความเสี่ยงก่อน" + "')", _
                                                       True)
                hdfCdd.Value = 0
            End If
            strCdd = hdfCdd.Value

            EnableKYC()

            If (grdShowAccount.SelectedIndex > -1) Then
                btnSave.Visible = True
            End If

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        Session("ds") = ds
    End Sub

#End Region

#Region "SetForm"

    Sub CreateControlMP()
        Dim img As Image = CType(Master.FindControl("imgHead"), Image)
        img.ImageUrl = "~/img/LogoNonIndi.jpg"

        Dim page As Label = CType(Master.FindControl("lblPage"), Label)
        page.Text = "ข้อมูล KYC"
    End Sub

    Sub VisbleKYC()
        If Request.QueryString("KYC") = 1 Then
            PanelDeposit.Visible = True
            PanelLoan.Visible = False
            PanelPN.Visible = False
        ElseIf Request.QueryString("KYC") = 2 Then
            PanelDeposit.Visible = False
            PanelLoan.Visible = True
            PanelPN.Visible = False
            btnSave.Visible = True
        ElseIf Request.QueryString("KYC") = 3 Then
            PanelDeposit.Visible = False
            PanelLoan.Visible = False
            PanelPN.Visible = True
        ElseIf Request.QueryString("KYC") = 5 Then
            PanelAccount.Visible = False
            PanelDeposit.Visible = False
            PanelLoan.Visible = False
            PanelPN.Visible = False
            btnSave.Visible = True
        Else
            PanelDeposit.Visible = True
            PanelLoan.Visible = True
            PanelPN.Visible = True 
            btnEdit.Visible = False
        End If
    End Sub

    Sub EnableKYC()
        If TypeKYC = 4 Then
            PanelDeposit.Enabled = False
            PanelLoan.Enabled = False
            PanelPN.Enabled = False
            PanelCBS.Enabled = False
            PanelKYC.Enabled = False
            Panel1.Enabled = False
            btnEdit.Visible = True
            'btnSave.Visible = False
            btnSave.Visible = False


            'PanelDepositSactionC.Visible = setVisiblePnlApp()
            txtDApproveDate.Visible = False
            txtApproveDate.Visible = True

            'If strCdd = "1" Then
            '    btnNext.Visible = False
            'Else
            '    btnNext.Visible = True
            '    btnNext.Text = "หน้าถัดไป"

            'End If
        Else
            PanelDeposit.Enabled = True
            PanelLoan.Enabled = True
            PanelPN.Enabled = True


            btnEdit.Visible = False
            ' btnSave.Visible = True

            'btnEdit.Visible = False

            'If strCdd = "1" Or strCdd = "" Or strCdd = "0" Then
            '    btnNext.Visible = False
            '    btnSave.Visible = True
            'Else
            '    btnNext.Visible = True
            '    btnSave.Visible = False
            'End If
            'PanelDepositSactionC.Visible = setVisiblePnlApp()
            txtDApproveDate.Visible = True
            txtApproveDate.Visible = False
        End If
        If Request.QueryString("KYC") = 5 Then
            btnSave.Visible = True

        End If

    End Sub


#End Region
    Public Function setVisiblePnlApp() As Boolean
        Dim result As Boolean = False

        'strCdd --> ความเสี่ยงปัจจุบัน
        'strCddOld --> ความเสี่ยงอดีต
        'ความเสี่ยงเป็น 2,3,3-->2,3-->1 ต้องโชว์ panal approve
        '
        If strCdd = 0 Then
            result = False
        Else
            If strCdd = 3 Then
                result = True
            ElseIf strCdd = 2 Then
                result = True
            ElseIf strCdd = 1 Then
                If (strCddOld = 2 Or strCddOld = 3) Then
                    result = True
                End If
            End If
        End If




        Return result
    End Function
#Region "Event Dropdrown"

    'Protected Sub ddlBusiness1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlBusiness1.SelectedIndexChanged
    '    indexBizChanging1()
    'End Sub

    Protected Sub ddlBusiness2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlBusiness2.SelectedIndexChanged
        indexBizChanging2()
    End Sub

    Protected Sub ddlBusiness3_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlBusiness3.SelectedIndexChanged
        indexBizChanging3()
    End Sub

#End Region

#Region "Event Button"

    'Protected Sub btnShow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnShow.Click
    '    Try
    '        If grdRef.SelectedIndex = -1 Then
    '            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" & Resources.resMsgKYC.msgSelect & "')", True)
    '        Else
    '            grdRef.SelectedIndex = -1
    '            btnShow.Attributes.Clear()
    '        End If
    '    Catch ex As Exception
    '        ScriptManager.RegisterStartupScript(UpdatePanel1, _
    '                                                        UpdatePanel1.GetType(), _
    '                                                        "clientScript", _
    '                                                        "alert('" + ex.Message.Replace("'", "") + "')", _
    '                                                        True)
    '    End Try
    'End Sub

    Protected Sub btnEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        Try
            Response.Redirect("NonIndividualKYC.aspx?KYC=" & Request.QueryString("KYC") & "&TypeKYC=3&CUSTSEQ=" & Request.QueryString("CUSTSEQ") & "&CDD=" & strCdd & "")
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
        Try
            If Not TypeKYC = 4 Then
                If checkBeforeSave() = True Then
                    saveData()
                    Response.Redirect("NonIndividualSectionC.aspx?KYC=" & Request.QueryString("KYC") & "&TypeKYC=" & TypeKYC & "&CUSTSEQ=" & CustSeq & "&CDD=" & strCdd & "")
                Else
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" & strMsg & "')", True)
                End If
            Else
                Response.Redirect("NonIndividualSectionC.aspx?KYC=" & Request.QueryString("KYC") & "&TypeKYC=" & TypeKYC & "&CUSTSEQ=" & CustSeq & "&CDD=" & strCdd & "")
            End If
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub
    Private Function checkApprove() As Boolean
        Try
            Dim result As Boolean = True

            If ddlDApprove.SelectedIndex = 0 Then
                result = False
                ImgSactionC3.ImageUrl = "~/img/icon/cancel.png"
                ImgSactionC3.Visible = True
                strMsg = strMsg + Resources.resMsgAlertKYC.NotStatusApprove + "\n"
            ElseIf ddlDApprove.SelectedValue = 2 Or ddlDApprove.SelectedValue = 3 Then
                ImgSactionC4.ImageUrl = "~/img/icon/accept.png"
                If txtDApproveNo.Text.Trim = "" Then
                    result = False
                    ImgSactionC4.ImageUrl = "~/img/icon/cancel.png"
                    strMsg = strMsg + Resources.resMsgAlertKYC.NotApproveNo + "\n"
                Else
                    Dim clsCheckApprove As clsCheckApprove = New clsCheckApprove
                    Dim strApp As String = clsCheckApprove.checkRightApprove(txtDApproveNo.Text, clsSessionMenuRight.getUserName, clsSessionMenuRight.getRoleGroup, clsSessionMenuRight.getBranchCode, clsSessionMenuRight.getZoneCode, clsSessionMenuRight.getDeptCode, txtApprovePassword.Text)

                    If strApp = "" Then
                        ImgSactionC4.ImageUrl = "~/img/icon/accept.png"
                    Else
                        result = False
                        ImgSactionC4.ImageUrl = "~/img/icon/cancel.png"
                        strMsg = strMsg + strApp + "\n"
                    End If
                End If
                If txtDApproveDate.Text = "" Then
                    result = False
                    ImgSactionC5.ImageUrl = "~/img/icon/cancel.png"
                    strMsg = strMsg + Resources.resMsgAlertKYC.NotApproveDate + "\n"
                Else
                    ImgSactionC5.ImageUrl = "~/img/icon/accept.png"
                End If

                ImgSactionC3.Visible = True
                ImgSactionC4.Visible = True
                ImgSactionC5.Visible = True

            End If

            Return result
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Protected Sub txtDValue_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDValue.TextChanged
        Try
            If Not txtDValue.Text = "" Then
                clsMoney = New clsMoney
                If clsMoney.insertComma(txtDValue.Text.Trim) = True Then
                    txtDValue.Text = clsMoney.insertComma(txtDValue.Text.Trim)
                    Image2.ImageUrl = "~/img/icon/accept.png"
                    Image2.Visible = True
                End If
                clsMoney = Nothing
            End If
        Catch ex As Exception
            condition = False
            Image2.ImageUrl = "~/img/icon/cancel.png"
            Image2.Visible = True
            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" & clsMoney.getStrMsg & "')", True)

        End Try
    End Sub
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try

            If Request.QueryString("CUSTSEQ") = 0 Then
                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" & Resources.resMsgKYC.msgCDD & "')", True)
                Exit Sub
            Else
                If Not TypeKYC = 4 Then
                    If Request.QueryString("KYC") = 5 Then
                        hdfKYCSeq.Value = "0"
                    End If

                    If Not hdfKYCSeq.Value = Nothing Then
                        If Request.QueryString("KYC") = 5 Then
                            If checkNPASave() = True Then
                                saveNPA()
                                ScriptManager.RegisterStartupScript(Me, Me.GetType(), "message", _
                                 "alert('" + Resources.resMsgKYC.msgSave + "');location.href = 'NonIndividualKYC.aspx?KYC=" & Request.QueryString("KYC") & "&TypeKYC=4&CUSTSEQ=" & CustSeq & "&CDD=" & strCdd & "&SelectGrd=" & 0 & "';", True)
                            Else
                                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" & strMsg & "')", True)
                            End If

                        Else
                            If checkBeforeSave() = True Then
                                If PanelDepositSactionC.Visible = True Then
                                    If checkApprove() = False Then
                                        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" & strMsg & "')", True)
                                        Exit Sub
                                    End If
                                End If

                                Call txtDValue_TextChanged(sender, e)
                                If condition = False Then
                                    condition = True
                                    Exit Sub
                                End If

                                ''''Prachak ''''''''''''''''''''''''''''''''
                                ''''''''''''''''''''''''''''''''''''''''''''
                                If Validate_For_All() = False Then Exit Sub
                                saveData()
                                ''''''''''''''''''''''''''''''''''''''''''''
                                ''''Prachak ''''''''''''''''''''''''''''''''

                                Dim intSelectGrid As Integer = grdShowAccount.SelectedIndex
                                ScriptManager.RegisterStartupScript(Me, Me.GetType(), "message", _
                                            "alert('" + Resources.resMsgKYC.msgSave + "');location.href = 'NonIndividualKYC.aspx?KYC=" & Request.QueryString("KYC") & "&TypeKYC=4&CUSTSEQ=" & CustSeq & "&CDD=" & strCdd & "&SelectGrd=" & intSelectGrid & "';", True)
                            Else
                                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" & strMsg & "')", True)
                            End If
                        End If

                    Else

                        If Request.QueryString("KYC") = 2 Then
                            If checkNPASave() = True Then
                                saveNPA()
                                ScriptManager.RegisterStartupScript(Me, Me.GetType(), "message", _
                                 "alert('" + Resources.resMsgKYC.msgSave + "');location.href = 'NonIndividualKYC.aspx?KYC=" & Request.QueryString("KYC") & "&TypeKYC=4&CUSTSEQ=" & CustSeq & "&CDD=" & strCdd & "&SelectGrd=" & 0 & "';", True)
                            End If

                        Else
                            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" & " กรุณาเลือกเลขที่บัญชี " & "')", True)

                        End If
                    End If
                Else
                    Response.Redirect("IndividualSearch.aspx?KYC=" & Request.QueryString("KYC") & "&CustSeq=" & CustSeq & "")
                End If
            End If

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub
    Function Validate_For_All() As Boolean
        Dim lvsOK As String

        Validate_For_All = False

        If Me.chkBO_SELF.Checked = False And Me.chkBO_OTH.Checked = False Then
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage",
                          "window.alert('กรูณาเลือกเป็นผู้รับผลประโยชน์เองหรือมีผู้รับผลประโยชน์อื่น');", True)
            Me.chkBO_SELF.Focus()
            Exit Function
        End If

        If CInt(Me.lblCount_grdBo.Text) = 0 Then
            ScriptManager.RegisterStartupScript(Page,
                          Page.GetType(), "alertMessage", "window.alert('กรูณาป้อนข้อมูลผู้รับผลประโยชน์');", True)
            Me.chkBO_OTH.Focus()
            Exit Function
        End If


        '''''''''''''''''''''''''''''''''''''''''''''''''''''
        '''''''''''''''''''''''''''''''''''''''''''''''''''''
        '''''''ตรวจสอบผู้รับผลประโยชน์เอง ''''''''''''''''''''''''''
        Dim mIDCard_No As String = ""

        If Me.chkBO_SELF.Checked = False Then

            For Each rowid As GridViewRow In Me.grdBo.Rows
                mIDCard_No = CType(rowid.FindControl("lbUNIQUEID_Bo"), Label).Text
                If mIDCard_No = txtBizNo.Text.Trim Then
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('ข้อมูลขัดแย้งกัน ! กรูณาเลือก-เป็นผู้รับผลประโยชน์เอง');", True)
                    Me.chkBO_SELF.Focus()
                    Exit Function
                End If

            Next
        Else

            lvsOK = "N"
            For Each rowid As GridViewRow In Me.grdBo.Rows
                mIDCard_No = CType(rowid.FindControl("lbUNIQUEID_Bo"), Label).Text
                If mIDCard_No = txtBizNo.Text.Trim Then
                    lvsOK = "Y"
                End If
            Next

            If lvsOK = "N" Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('ข้อมูลขัดแย้งกัน ! กรูณาเพิ่มข้อมูลผู้รับผลประโยชน์เอง');", True)
                Me.btAddBo_SELF.Focus()
                Exit Function
            End If

        End If


        ''''''''''''''''''''''''''''''''''''''''''
        ''''''''''''''''''''''''''''''''''''''''''
        ''''''''''''''''''''''''''''''''''''''''''
        '''''''ตรวจสอบผู้รับผลประโยชน์อื่น '''''''''''''''
        If Me.chkBO_OTH.Checked = False Then
            mIDCard_No = ""
            For Each rowid As GridViewRow In Me.grdBo.Rows
                mIDCard_No = CType(rowid.FindControl("lbUNIQUEID_Bo"), Label).Text
                If mIDCard_No <> txtBizNo.Text.Trim Then
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('ข้อมูลขัดแย้งกัน ! กรูณาเลือก-เป็นผู้รับผลประโยชน์อื่น');", True)
                    Me.chkBO_SELF.Focus()
                    Exit Function
                End If
            Next

        Else
            lvsOK = "N"

            For Each rowid As GridViewRow In Me.grdBo.Rows
                mIDCard_No = CType(rowid.FindControl("lbUNIQUEID_Bo"), Label).Text
                If mIDCard_No <> txtBizNo.Text.Trim Then
                    lvsOK = "Y"
                    Exit For
                End If
            Next

            If lvsOK = "N" Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('ข้อมูลขัดแย้งกัน ! กรูณาเพิ่มข้อมูลผู้รับผลประโยชน์อื่่น');", True)
                Me.btAddBo_SELF.Focus()
                Exit Function
            End If

        End If


        Validate_For_All = True

    End Function
    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Try
            Response.Redirect("NonIndividualSearch.aspx?KYC=" & Request.QueryString("KYC") & "&CustSeq=" & CustSeq & "")
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

#End Region

#Region "Function"

    Private Sub setGrid()
        If Not grdRef.Rows.Count = 0 Then
            grdRef.HeaderRow.Cells(7).Visible = False
            For i As Integer = 0 To grdRef.Rows.Count - 1
                grdRef.Rows(i).Cells(7).Visible = False
            Next
        End If
    End Sub

    Private Sub loadDataToDropDown()
        Try
            Dim clsDropDown As New clsMasterDropDown

            clsDropDown.setSubTypeToDdl(ddlBizType)
            clsDropDown.setPrefixTHToDdl(ddlPrefix, 2)
            clsDropDown.setNationalityToDdl(ddlNationality)

            clsDropDown.setBusinessToDdl(ddlBusiness2)
            clsDropDown.setBusinessToDdl(ddlBusiness3)
            clsDropDown.setCountyToDdl(ddlIncomeSource2)
            clsDropDown.setCountyToDdl(ddlIncomeSource3)

            'clsDropDown.setProvice(ddlProvice)

            ''''Prachak ''''''''''''''''''''''
            clsDropDown.setOtherDoc_To_Ddl(Me.DdOtherID)
            ''''Prachak '''''''''''''''''''''''''''''''''''

            clsDropDown.setCheckToDdl(ddlCheckName, 3, "")

            clsDropDown.setApproveToDdl(ddlDApprove)
            clsDropDown = Nothing
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub loadDataAddr(ByVal province As String, ByVal amphor As String, ByVal tambol As String, ByVal statusAddr As String)
        Dim clsDropDown As New clsMasterDropDown
        If (statusAddr = 1) Then 'formal
            If province = "" Then
                clsDropDown.setProvince(ddlFormalProvice)
            Else
                clsDropDown.setProvince(ddlFormalProvice, province)
                If Not (ddlFormalProvice.SelectedIndex = 0) Then
                    clsDropDown.setAmphor(ddlFormalAmphor, province, amphor)

                End If
                If Not (ddlFormalProvice.SelectedIndex = 0) Then
                    clsDropDown.setTambol(ddlFormalTambol, amphor, tambol)
                End If

            End If

        End If


    End Sub

    Private Sub BindingDataCBS() 'ข้อมูลพื้นฐานลูกค้า 
        Try
            Dim qryA = (From cust In KYCEntity.MCUSTOMER _
                                    Where cust.CUSTSEQ = CustSeq
                                    Select cust).ToList

            If qryA(0).CUSTCODE = Nothing Then
                lblCustCode.Text = "เลขประจำตัวลูกค้า : -"
            Else
                lblCustCode.Text = "เลขประจำตัวลูกค้า : " + qryA(0).CUSTCODE.ToString
            End If

            If Not qryA(0).THPREFIX Is Nothing Then
                ddlPrefix.SelectedValue = qryA(0).THPREFIX
            End If
            '''''''Prachak ''''''''''''''''''''''''''''''''''
            Me.lblCIF_CBS_NO.Text = qryA(0).CIF_CBS_NO
            Me.txtCIF_FDR_NO.Text = qryA(0).CIF_FDR_NO
            Me.txtCIF_NPA_NO.Text = qryA(0).CIF_NPA_NO

            '''''''Prachak ''''''''''''''''''''''''''''''''''
            Me.lblFILTER_RESULT_CUST.Text = qryA(0).FILTER_RESULT
            Me.lblRISK_LEVEL_CUST.Text = qryA(0).RISK_LEVEL

            ''''รายได้เฉลี่ยต่อเดือน '''''''''''''''''''''''''''''''''''''''''''''
            If Not qryA(0).AVR_REVENUE Is Nothing Then
                Me.ddlAVR_REVENUE.SelectedValue = qryA(0).AVR_REVENUE
            End If
            ''''ค่าใช้จ่ายเฉลี่ยต่อเดือน''''''''''''''''''''''''''''''''''''''''''''
            If Not qryA(0).AVR_EXPENSE Is Nothing Then
                Me.ddlAVR_EXPENSE.SelectedValue = qryA(0).AVR_EXPENSE
            End If

            '''''''Prachak ''''''''''''''''''''''''''''''''''
            Me.lblF_POLITIC.Text = qryA(0).F_POLITIC
            If Me.lblF_POLITIC.Text = "Y" Then
                Me.optPOL_CONNECT_Y.Checked = True
            Else
                Me.optPOL_CONNECT_N.Checked = True

            End If

            ''''''''''''''''''''''''''''''''''''
            If Not qryA(0).TYPEEVIDENCE Is Nothing Then
                DdOtherID.SelectedValue = qryA(0).TYPEEVIDENCE
            End If
            Me.txtTAX_ID.Text = qryA(0).TAX_ID

            Me.txtTYPEEVIDENCE_NO.Text = qryA(0).TYPEEVIDENCE_NO
            Me.lbBIRTH_DATE_TH.Text = qryA(0).BIRTH_DATE_TH
            Me.txtBIRTH_DATE_TH_DD.Text = (Mid(Me.lbBIRTH_DATE_TH.Text, 1, 2))
            Me.txtBIRTH_DATE_TH_MM.Text = (Mid(Me.lbBIRTH_DATE_TH.Text, 4, 2))
            Me.txtBIRTH_DATE_TH_YYYY.Text = (Mid(Me.lbBIRTH_DATE_TH.Text, 7, 4))


            Me.lbBIRTH_DATE_EN.Text = qryA(0).BIRTH_DATE_EN
            Me.txtBIRTH_DATE_EN_DD.Text = (Mid(Me.lbBIRTH_DATE_EN.Text, 1, 2))
            Me.txtBIRTH_DATE_EN_MM.Text = (Mid(Me.lbBIRTH_DATE_EN.Text, 4, 2))
            Me.txtBIRTH_DATE_EN_YYYY.Text = (Mid(Me.lbBIRTH_DATE_EN.Text, 7, 4))


            Me.txtOTH_NAT_ADD_1.Text = qryA(0).OTH_NAT_ADD_1
            Me.txtOTH_NAT_ADD_2.Text = qryA(0).OTH_NAT_ADD_2
            Me.txtOTH_NAT_ZIP.Text = qryA(0).OTH_NAT_ZIP
            Me.txtOTH_NAT_STREET.Text = qryA(0).OTH_NAT_STREET
            Me.txtOTH_NAT_PHN.Text = qryA(0).OTH_NAT_PHN
            Me.txtOTH_NAT_PHN_EXT.Text = qryA(0).OTH_NAT_PHN_EXT
            Me.txtOTH_NAT_MOBILE.Text = qryA(0).OTH_NAT_MOBILE
            Me.txtOTH_NAT_FAX.Text = qryA(0).OTH_NAT_FAX
            Me.txtOTH_NAT_EMAIL.Text = qryA(0).OTH_NAT_EMAIL


            txtNameTH.Text = qryA(0).THFIRSTNAME
            txtNameEN.Text = qryA(0).ENFIRSTNAME
            txtBizNo.Text = qryA(0).UNIQUEID
            If Not qryA(0).NATIONALITY Is Nothing Then
                ddlNationality.SelectedValue = qryA(0).NATIONALITY
            End If
            If Not qryA(0).CUSTSUBTYPE Is Nothing Then
                ddlBizType.SelectedValue = qryA(0).CUSTSUBTYPE
            End If
            If Not qryA(0).BIZTYPE2 Is Nothing Then
                ddlBusiness2.SelectedValue = qryA(0).BIZTYPE2
            End If
            txtBizOther2.Text = qryA(0).BIZTYPEOTHER2
            If Not qryA(0).BIZTYPE3 Is Nothing Then
                ddlBusiness3.SelectedValue = qryA(0).BIZTYPE3
            End If
            txtBizOther3.Text = qryA(0).BIZTYPEOTHER3

            txtDes1.Text = qryA(0).BIZDESC1
            txtDes2.Text = qryA(0).BIZDESC2
            txtDes3.Text = qryA(0).BIZDESC3

            'txtFormalAddr.Text = qryA(0).FORMALADDR
            'txtFormalPhone1.Text = qryA(0).FORMALPHONE1
            'txtFormalPhone2.Text = qryA(0).FORMALPHONE2
            'txtFormalPhone3.Text = qryA(0).FORMALPHONE3
            'txtFormalFax.Text = qryA(0).FORMALFAX
            loadDataAddr(qryA(0).FORMALPROVINCE, qryA(0).FORMALAMPHOR, qryA(0).FORMALTAMBOL, 1)
            txtFormalAddress.Text = qryA(0).FORMALNO
            txtFormalMoo.Text = qryA(0).FORMALMOO
            txtFormalStreet.Text = qryA(0).FORMALSTREET
            txtFormalSoi.Text = qryA(0).FORMALSOIDESC
            txtFormalBuilding.Text = qryA(0).FORMALBUILDING
            txtFormalPhone1_1.Text = qryA(0).FORMALPHONE1
            txtFormalPhone2_2.Text = qryA(0).FORMALPHONE2
            txtFormalPhone3_3.Text = qryA(0).FORMALPHONE3
            txtFormalFax1_1.Text = qryA(0).FORMALFAX
            txtFormalPostCode.Text = qryA(0).FORMALZIP

            If Not qryA(0).INCOMESOURCE2 Is Nothing Then
                ddlIncomeSource2.SelectedValue = qryA(0).INCOMESOURCE2
            End If
            If Not qryA(0).INCOMESOURCE3 Is Nothing Then
                ddlIncomeSource3.SelectedValue = qryA(0).INCOMESOURCE3
            End If

            If Not qryA(0).COMPCHECK1 Is Nothing Then
                If qryA(0).COMPCHECK1 = 1 Then
                    chkRegister.Items(0).Selected = True
                ElseIf qryA(0).COMPCHECK1 = 2 Then
                    chkRegister.Items(1).Selected = True
                ElseIf qryA(0).COMPCHECK1 = 3 Then
                    chkRegister.Items(2).Selected = True
                End If
            End If

            If Not qryA(0).CHECKADDRESS Is Nothing Then
                ddlCheckName.SelectedValue = qryA(0).CHECKADDRESS
            End If

            Dim riskLevel As String = ""
            riskLevel = "ระดับความเสี่ยงลูกค้า : " + qryA(0).AMLCDD.ToString

            hdfCdd.Value = qryA(0).AMLCDD.ToString
            If qryA(0).AMLCDDBEFORE.ToString = "" Then
                hdfCddOld.Value = 0

            Else
                hdfCddOld.Value = qryA(0).AMLCDDBEFORE.ToString

            End If
            'If qryA(0).AMLCDDOVERVIEW > 1 Then
            If Not qryA(0).AMLREASON Is Nothing Then
                riskLevel += " (เหตุผล : " + qryA(0).AMLREASON.ToString + ")"
            End If
            'End If

            If qryA(0).AMLCDD.ToString = "1" Then
                lblRiskLevel.ForeColor = Drawing.Color.Green
            ElseIf qryA(0).AMLCDD.ToString = "2" Then
                lblRiskLevel.ForeColor = Drawing.Color.Orange
            ElseIf qryA(0).AMLCDD.ToString = "3" Then
                lblRiskLevel.ForeColor = Drawing.Color.Red
            End If

            lblRiskLevel.Text = riskLevel

            If Not qryA(0).TRANTYPE Is Nothing Then
                If qryA(0).TRANTYPE = 2 Then 'ลูกค้าขาจร
                    PanelCBS.Enabled = True
                    CustCasual()
                    hdfTranType.Value = 2
                ElseIf Request.QueryString("KYC") = 3 And qryA(0).TRANTYPE = 1 Then
                    PanelCBS.Enabled = True
                    CustCasual()
                    hdfTranType.Value = 2
                Else
                    hdfTranType.Value = 0
                End If
            End If

            qryA = Nothing
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub loadKYCAccount()
        Dim clsDataTable As New clsLINQToDataTable
        Try
            '  Dim convertDate As clsDateFormat = New clsDateFormat
            Dim intANSWERTYPE As Integer = 0

            If Request.QueryString("KYC") = 1 Then
                intANSWERTYPE = 2
            ElseIf Request.QueryString("KYC") = 2 Then
                intANSWERTYPE = 4
            ElseIf Request.QueryString("KYC") = 3 Then
                intANSWERTYPE = 6
            ElseIf Request.QueryString("KYC") = 4 Then
                intANSWERTYPE = 0
            End If

            If ds.Tables.Contains("RKYC") Then
                ds.Tables.Remove("RKYC")
            End If

            Dim qryRKYc = From tblKYC In KYCEntity.RKYC
             Where tblKYC.CUSTSEQ = CustSeq _
               And tblKYC.ACCOUNTNO <> ""
            Select tblKYC.ANSWERTYPE,
                   tblKYC.ACCOUNTNO,
                   tblKYC.APPROVECODE,
                   tblKYC.ACCOUNTNAME,
                   tblKYC.KYCSEQ,
                   tblKYC.WORKERSTATUS,
                   tblKYC.KYCCOMPLETE,
                   tblKYC.APPROVENO,
                   tblKYC.BRANCHCODE,
                   tblKYC.UPDATEBY,
                   tblKYC.F_JOIN_ACCT,
                   FLAG_SELF_BO = If(tblKYC.FLAG_SELF_BO = "Y", True, False),
                   FLAG_OTHP_BO = If(tblKYC.FLAG_OTHP_BO = "Y", True, False),
                   ACTIVE = If(tblKYC.ACTIVE = "Y", True, False)
          Order By KYCSEQ


            If Not intANSWERTYPE = 0 Then
                qryRKYc = qryRKYc.Where(Function(fn) fn.ANSWERTYPE = intANSWERTYPE)
            End If

            Dim aa As String = Request.QueryString("SelectGrd")
            ds.Tables.Add(clsDataTable.LINQToDataTable(qryRKYc, "RKYC").Copy)
            grdShowAccount.DataSource = ds.Tables("RKYC")
            grdShowAccount.DataBind()

            If Not aa Is Nothing Then
                grdShowAccount.SelectedIndex = CInt(aa)
                Call grdShowAccount_SelectedIndexChanged(Me, Nothing)
            End If
            'ds.Tables.Add(clsDataTable.LINQToDataTable(qryRKYc, "RKYC").Copy)
            'grdShowAccount.DataSource = ds.Tables("RKYC")
            'grdShowAccount.DataBind()


            If Not acc = "" Then
                For i As Integer = 0 To grdShowAccount.Rows.Count - 1
                    If grdShowAccount.Rows(i).Cells(1).Text = acc Then
                        grdShowAccount.SelectedIndex = i
                        If (grdShowAccount.SelectedIndex = intBeforeItemIndex) Then
                            grdShowAccount.SelectedIndex = -1
                            resetData()

                            'btnAdd.Visible = True
                            'btnAddOld.Visible = False
                        Else
                            Dim strKYCSEQ As String = CType(grdShowAccount.Rows(grdShowAccount.SelectedIndex).FindControl("lblKYCSeq"), Label).Text
                            Dim strAccName As String = CType(grdShowAccount.Rows(grdShowAccount.SelectedIndex).FindControl("lblAccountName"), Label).Text
                            hdfKYCSeq.Value = strKYCSEQ
                            TxtAccNo.Text = grdShowAccount.Rows(grdShowAccount.SelectedIndex).Cells(1).Text.Trim
                            If grdShowAccount.Rows(grdShowAccount.SelectedIndex).Cells(2).Text.Trim = "" Then
                                txtAccName.Text = txtNameTH.Text
                            Else
                                txtAccName.Text = grdShowAccount.Rows(grdShowAccount.SelectedIndex).Cells(2).Text.Trim
                            End If


                            If Request.QueryString("KYC") = 1 Then
                                BindingDataKYCDeposit()
                            ElseIf Request.QueryString("KYC") = 2 Then
                                BindingDataKYCLoan()
                            ElseIf Request.QueryString("KYC") = 3 Then
                                BindingDataKYCPN()
                                '  PanelCBS.Enabled = True
                            Else
                                intANSWERTYPE = CType(grdShowAccount.Rows(grdShowAccount.SelectedIndex).FindControl("lblAnswerType"), Label).Text
                                ' VisbleKYC()
                                If intAnswerType = 1 Or intAnswerType = 2 Then
                                    PanelDeposit.Visible = True
                                    PanelLoan.Visible = False
                                    PanelPN.Visible = False
                                    BindingDataKYCDeposit()
                                ElseIf intAnswerType = 3 Or intAnswerType = 4 Then
                                    PanelDeposit.Visible = False
                                    PanelLoan.Visible = True
                                    PanelPN.Visible = False
                                    BindingDataKYCLoan()
                                ElseIf intAnswerType = 5 Or intAnswerType = 6 Then
                                    PanelDeposit.Visible = False
                                    PanelLoan.Visible = False
                                    PanelPN.Visible = True
                                    BindingDataKYCPN()
                                End If
                            End If


                        End If
                        Exit For
                    End If
                Next
            End If

        Catch ex As Exception

        End Try


    End Sub
    Private Sub CustCasual()
        Try
            If Request.QueryString("KYC") = 3 Then
                '  lblCustCode.Text = "ลูกค้าตั๋ว"
            Else

                lblCustCode.Text = "ลูกค้าขาจร"
            End If
            ddlPrefix.Enabled = False
            txtNameTH.Enabled = False
            txtNameEN.Enabled = False
            ddlBizType.Enabled = False
            txtBizNo.Enabled = False
            ddlNationality.Enabled = False
            ddlBusiness2.Enabled = False
            txtBizOther2.Enabled = False
            ddlBusiness3.Enabled = False
            txtBizOther3.Enabled = False
            txtDes1.Enabled = False
            txtDes2.Enabled = False
            txtDes3.Enabled = False

            txtFormalAddress.BackColor = Drawing.Color.FromArgb(255, 204, 204)
            ddlFormalProvice.BackColor = Drawing.Color.FromArgb(255, 204, 204)
            ddlFormalAmphor.BackColor = Drawing.Color.FromArgb(255, 204, 204)
            ddlFormalTambol.BackColor = Drawing.Color.FromArgb(255, 204, 204)
            txtFormalPostCode.BackColor = Drawing.Color.FromArgb(255, 204, 204)

            ddlIncomeSource2.Enabled = False
            ddlIncomeSource3.Enabled = False
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub BindingDataKYCDeposit(Optional ByVal strAccount As String = "")
        Try
            Dim intkycSeq As Integer = hdfKYCSeq.Value
            Dim qryTblKYC = From kyc In KYCEntity.RKYC
                          Where kyc.ANSWERTYPE = 2
                          Select kyc

            If Not strAccount = "" Then
                qryTblKYC = qryTblKYC.Where(Function(fn) fn.ACCOUNTNO = strAccount.Trim)
                intCountSearch = qryTblKYC.Count
            Else
                qryTblKYC = qryTblKYC.Where(Function(fn) fn.CUSTSEQ = CustSeq And fn.KYCSEQ = intkycSeq)

            End If
            Dim qryKYC = qryTblKYC.ToList


            If Not qryKYC.Count = 0 Then
                'If Not qryKYC(0).CHECKADDRESS Is Nothing Then
                '    ddlCheckAddress.SelectedValue = qryKYC(0).CHECKADDRESS
                'End If
                If Not strAccount = "" Then
                    txtAccName.Text = qryKYC(0).ACCOUNTNAME
                End If

                If qryKYC(0).ANSWER101 = 0 Then
                    chkDAcc1.Checked = True
                End If
                If qryKYC(0).ANSWER102 = 0 Then
                    chkDAcc2.Checked = True
                End If
                If qryKYC(0).ANSWER103 = 0 Then
                    chkDAcc3.Checked = True
                End If
                If qryKYC(0).ANSWER104 = 0 Then
                    chkDAcc4.Checked = True
                    txtDAccOther.Text = qryKYC(0).ANSWER199
                End If
                If qryKYC(0).ANSWER201 = 0 Then
                    chkDSource1.Checked = True
                End If
                If qryKYC(0).ANSWER202 = 0 Then
                    chkDSource2.Checked = True
                End If
                If qryKYC(0).ANSWER203 = 0 Then
                    chkDSource3.Checked = True
                End If
                If qryKYC(0).ANSWER204 = 0 Then
                    chkDSource4.Checked = True
                End If
                If qryKYC(0).ANSWER205 = 0 Then
                    chkDSource5.Checked = True
                    txtDSourceOther.Text = qryKYC(0).ANSWER299
                End If


                If qryKYC(0).ANSWER301 = 0 Then 'มีความสัมพันธ์
                    rdoDRef.Items(0).Selected = True
                    rdoDRef.Items(1).Selected = False
                Else
                    rdoDRef.Items(0).Selected = False
                    rdoDRef.Items(1).Selected = True
                End If

                ddlDBoard1.SelectedValue = qryKYC(0).REF101
                ddlDBoard2.SelectedValue = qryKYC(0).REF102
                ddlDRef1.SelectedValue = qryKYC(0).REF103
                txtDRef1.Text = qryKYC(0).REF104
                ddlDBoard3.SelectedValue = qryKYC(0).REF201
                ddlDBoard4.SelectedValue = qryKYC(0).REF202
                ddlDRef2.SelectedValue = qryKYC(0).REF203
                txtDRef2.Text = qryKYC(0).REF204

                If Not qryKYC(0).ANSWER401 Is Nothing Then
                    ddlDDeposit.SelectedValue = qryKYC(0).ANSWER401
                End If
                If Not qryKYC(0).ANSWER501 Is Nothing Then
                    ddlDMoney.SelectedValue = qryKYC(0).ANSWER501
                End If

                'If Not qryKYC(0).APPROVECODE Is Nothing Then
                '    btnNext.Visible = True
                '    btnSave.Visible = False
                'End If

                'เพิ่มเติม
                If PanelDepositSactionC.Visible = True And strAccount = "" Then
                    If qryKYC(0).ASSET1 = 0 Then
                        chkDAsset1.Checked = True
                    End If
                    If qryKYC(0).ASSET2 = 0 Then
                        chkDAsset2.Checked = True
                    End If
                    If qryKYC(0).ASSET3 = 0 Then
                        chkDAsset3.Checked = True
                    End If
                    If qryKYC(0).ASSET4 = 0 Then
                        chkDAsset4.Checked = True
                    End If
                    If qryKYC(0).ASSET5 = 0 Then
                        chkDAsset5.Checked = True
                        txtDAssetOther.Text = qryKYC(0).ASSETOTHER
                    End If

                    If Not qryKYC(0).APPROVECODE Is Nothing Then
                        ddlDApprove.SelectedValue = qryKYC(0).APPROVECODE
                    End If
                    'txtDApprove.Text = qryKYC(0).APPROVENAME
                    txtDApproveNo.Text = qryKYC(0).APPROVENO
                    convertDate = New clsDateFormat
                    If qryKYC(0).APPROVEDATE Is Nothing Then
                        txtDApproveDate.Text = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, Now.Date)
                    ElseIf Not qryKYC(0).APPROVEDATE Is Nothing Then
                        txtDApproveDate.Text = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, qryKYC(0).APPROVEDATE)
                        txtApproveDate.Text = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, qryKYC(0).APPROVEDATE)
                    End If
                    convertDate = Nothing

                    If Not qryKYC(0).TOTALASSET Is Nothing Then
                        clsMoney = New clsMoney
                        txtDValue.Text = clsMoney.insertComma(qryKYC(0).TOTALASSET)
                        clsMoney = Nothing
                    End If

                    If Not qryKYC(0).ISENTITYCOMP Is Nothing Then
                        rdoDEntity.SelectedIndex = qryKYC(0).ISENTITYCOMP
                    End If
                    If Not qryKYC(0).ISHOLDINGCOMP Is Nothing Then
                        rdoDHold.SelectedIndex = qryKYC(0).ISHOLDINGCOMP
                    End If
                End If


            Else
                'If Not Request.QueryString("KYC") = 4 Then
                '    ScriptManager.RegisterStartupScript(Me, Me.GetType(), "message", _
                '                         "alert('" + Resources.resMsgKYC.msgCDD + "');location.href = 'NonIndividualCDD.aspx?KYC=" & Request.QueryString("KYC") & "&TypeKYC=2&CUSTSEQ=" & CustSeq & "';", True)
                '    Exit Sub
                'End If

            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub BindingDataKYCLoan(Optional ByVal strAccount As String = "")
        Try
            Dim intkycSeq As Integer = hdfKYCSeq.Value
            Dim qryTblKYC = From kyc In KYCEntity.RKYC
                          Where kyc.ANSWERTYPE = 4
                          Select kyc

            If Not strAccount = "" Then
                qryTblKYC = qryTblKYC.Where(Function(fn) fn.ACCOUNTNO = strAccount.Trim)
                intCountSearch = qryTblKYC.Count
            Else
                qryTblKYC = qryTblKYC.Where(Function(fn) fn.CUSTSEQ = CustSeq And fn.KYCSEQ = intkycSeq)

            End If
            Dim qryKYC = qryTblKYC.ToList


            If Not qryKYC.Count = 0 Then
                If Not strAccount = "" Then
                    txtAccName.Text = qryKYC(0).ACCOUNTNAME
                End If
                'If Not qryKYC(0).CHECKADDRESS Is Nothing Then
                '    ddlCheckAddress.SelectedValue = qryKYC(0).CHECKADDRESS
                'End If

                If qryKYC(0).ANSWER101 = 0 Then
                    chkLAcc1.Checked = True
                End If
                If qryKYC(0).ANSWER102 = 0 Then
                    chkLAcc2.Checked = True
                End If
                If qryKYC(0).ANSWER103 = 0 Then
                    chkLAcc3.Checked = True
                End If
                If qryKYC(0).ANSWER104 = 0 Then
                    chkLAcc4.Checked = True
                End If
                If qryKYC(0).ANSWER105 = 0 Then
                    chkLAcc5.Checked = True
                End If
                If qryKYC(0).ANSWER106 = 0 Then
                    chkLAcc6.Checked = True
                End If
                If qryKYC(0).ANSWER107 = 0 Then
                    chkLAcc7.Checked = True
                    txtLAccOther.Text = qryKYC(0).ANSWER199
                End If


                ddlLBoard1.SelectedValue = qryKYC(0).REF101
                ddlLBoard2.SelectedValue = qryKYC(0).REF102
                ddlLRef1.SelectedValue = qryKYC(0).REF103
                txtLRef1.Text = qryKYC(0).REF104
                ddlLBoard3.SelectedValue = qryKYC(0).REF201
                ddlLBoard4.SelectedValue = qryKYC(0).REF202
                ddlLRef2.SelectedValue = qryKYC(0).REF203
                txtLRef1.Text = qryKYC(0).REF204
                ddlLBoard5.SelectedValue = qryKYC(0).REF301
                ddlLBoard6.SelectedValue = qryKYC(0).REF302
                ddlLRef3.SelectedValue = qryKYC(0).REF303
                txtLRef3.Text = qryKYC(0).REF304
                ddlLBoard7.SelectedValue = qryKYC(0).REF401
                ddlLBoard8.SelectedValue = qryKYC(0).REF402
                ddlLRef4.SelectedValue = qryKYC(0).REF403
                txtLRef4.Text = qryKYC(0).REF404
                ddlLBoard9.SelectedValue = qryKYC(0).REF501
                ddlLBoard10.SelectedValue = qryKYC(0).REF502
                ddlLRef5.SelectedValue = qryKYC(0).REF503
                txtLRef5.Text = qryKYC(0).REF504

                'If Not qryKYC(0).APPROVECODE Is Nothing Then
                '    btnNext.Visible = True
                '    btnSave.Visible = False
                'End If
                If PanelDepositSactionC.Visible = True And strAccount = "" Then
                    If qryKYC(0).ASSET1 = 0 Then
                        chkDAsset1.Checked = True
                    End If
                    If qryKYC(0).ASSET2 = 0 Then
                        chkDAsset2.Checked = True
                    End If
                    If qryKYC(0).ASSET3 = 0 Then
                        chkDAsset3.Checked = True
                    End If
                    If qryKYC(0).ASSET4 = 0 Then
                        chkDAsset4.Checked = True
                    End If
                    If qryKYC(0).ASSET5 = 0 Then
                        chkDAsset5.Checked = True
                        txtDAssetOther.Text = qryKYC(0).ASSETOTHER
                    End If

                    If Not qryKYC(0).APPROVECODE Is Nothing Then
                        ddlDApprove.SelectedValue = qryKYC(0).APPROVECODE
                    End If
                    'txtDApprove.Text = qryKYC(0).APPROVENAME
                    txtDApproveNo.Text = qryKYC(0).APPROVENO
                    convertDate = New clsDateFormat
                    If qryKYC(0).APPROVEDATE Is Nothing Then
                        txtDApproveDate.Text = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, Now.Date)
                    ElseIf Not qryKYC(0).APPROVEDATE Is Nothing Then
                        txtDApproveDate.Text = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, qryKYC(0).APPROVEDATE)
                        txtApproveDate.Text = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, qryKYC(0).APPROVEDATE)
                    End If
                    convertDate = Nothing

                    If Not qryKYC(0).TOTALASSET Is Nothing Then
                        clsMoney = New clsMoney
                        txtDValue.Text = clsMoney.insertComma(qryKYC(0).TOTALASSET)
                        clsMoney = Nothing
                    End If

                    If Not qryKYC(0).ISENTITYCOMP Is Nothing Then
                        rdoDEntity.SelectedIndex = qryKYC(0).ISENTITYCOMP
                    End If
                    If Not qryKYC(0).ISHOLDINGCOMP Is Nothing Then
                        rdoDHold.SelectedIndex = qryKYC(0).ISHOLDINGCOMP
                    End If

                End If


            Else
                'If Not Request.QueryString("KYC") = 4 Then
                '    ScriptManager.RegisterStartupScript(Me, Me.GetType(), "message", _
                '                          "alert('" + Resources.resMsgKYC.msgCDD + "');location.href = 'NonIndividualCDD.aspx?KYC=" & Request.QueryString("KYC") & "&TypeKYC=2&CUSTSEQ=" & CustSeq & "';", True)
                '    Exit Sub
                'End If

            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub BindingDataKYCPN(Optional ByVal strAccount As String = "")
        Try
            Dim intkycSeq As Integer = hdfKYCSeq.Value
            Dim qryTblKYC = From kyc In KYCEntity.RKYC
                          Where kyc.ANSWERTYPE = 6
                          Select kyc

            If Not strAccount = "" Then
                qryTblKYC = qryTblKYC.Where(Function(fn) fn.ACCOUNTNO = strAccount.Trim)
                intCountSearch = qryTblKYC.Count
            Else
                qryTblKYC = qryTblKYC.Where(Function(fn) fn.CUSTSEQ = CustSeq And fn.KYCSEQ = intkycSeq)

            End If
            Dim qryKYC = qryTblKYC.ToList



            If Not qryKYC.Count = 0 Then
                If Not strAccount = "" Then
                    txtAccName.Text = qryKYC(0).ACCOUNTNAME
                End If
                'If Not qryKYC(0).CHECKADDRESS Is Nothing Then
                '    ddlCheckAddress.SelectedValue = qryKYC(0).CHECKADDRESS
                'End If

                If qryKYC(0).ANSWER101 = 0 Then
                    chkPAcc1.Checked = True
                End If
                If qryKYC(0).ANSWER102 = 0 Then
                    chkPAcc2.Checked = True
                End If
                If qryKYC(0).ANSWER103 = 0 Then
                    chkPAcc3.Checked = True
                End If
                If qryKYC(0).ANSWER104 = 0 Then
                    chkPAcc4.Checked = True
                    txtPAccOther.Text = qryKYC(0).ANSWER199
                End If

                If qryKYC(0).ANSWER201 = 0 Then
                    chkPSource1.Checked = True
                End If
                If qryKYC(0).ANSWER202 = 0 Then
                    chkPSource2.Checked = True
                End If
                If qryKYC(0).ANSWER203 = 0 Then
                    chkPSource3.Checked = True
                End If
                If qryKYC(0).ANSWER204 = 0 Then
                    chkPSource4.Checked = True
                End If
                If qryKYC(0).ANSWER205 = 0 Then
                    chkPSource5.Checked = True
                    txtPSourceOther.Text = qryKYC(0).ANSWER299
                End If

                If qryKYC(0).ANSWER301 = 0 Then 'มีความสัมพันธ์
                    rdoPRef.Items(0).Selected = True
                    rdoPRef.Items(1).Selected = False
                Else
                    rdoPRef.Items(0).Selected = False
                    rdoPRef.Items(1).Selected = True
                End If

                ddlPBoard1.SelectedValue = qryKYC(0).REF101
                ddlPBoard2.SelectedValue = qryKYC(0).REF102
                ddlPRef1.SelectedValue = qryKYC(0).REF103
                txtPRef1.Text = qryKYC(0).REF104
                ddlPBoard3.SelectedValue = qryKYC(0).REF201
                ddlPBoard4.SelectedValue = qryKYC(0).REF202
                ddlPRef2.SelectedValue = qryKYC(0).REF203
                txtPRef2.Text = qryKYC(0).REF204

                If Not qryKYC(0).ANSWER401 Is Nothing Then
                    ddlPDeposit.SelectedValue = qryKYC(0).ANSWER401
                End If
                If Not qryKYC(0).ANSWER501 Is Nothing Then
                    ddlPMoney.SelectedValue = qryKYC(0).ANSWER501
                End If

                'If Not qryKYC(0).APPROVECODE Is Nothing Then
                '    btnNext.Visible = True
                '    btnSave.Visible = False
                'End If

                If PanelDepositSactionC.Visible = True And strAccount = "" Then
                    If qryKYC(0).ASSET1 = 0 Then
                        chkDAsset1.Checked = True
                    End If
                    If qryKYC(0).ASSET2 = 0 Then
                        chkDAsset2.Checked = True
                    End If
                    If qryKYC(0).ASSET3 = 0 Then
                        chkDAsset3.Checked = True
                    End If
                    If qryKYC(0).ASSET4 = 0 Then
                        chkDAsset4.Checked = True
                    End If
                    If qryKYC(0).ASSET5 = 0 Then
                        chkDAsset5.Checked = True
                        txtDAssetOther.Text = qryKYC(0).ASSETOTHER
                    End If

                    If Not qryKYC(0).APPROVECODE Is Nothing Then
                        ddlDApprove.SelectedValue = qryKYC(0).APPROVECODE
                    End If
                    'txtDApprove.Text = qryKYC(0).APPROVENAME
                    txtDApproveNo.Text = qryKYC(0).APPROVENO
                    convertDate = New clsDateFormat
                    If qryKYC(0).APPROVEDATE Is Nothing Then
                        txtDApproveDate.Text = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, Now.Date)
                    ElseIf Not qryKYC(0).APPROVEDATE Is Nothing Then
                        txtDApproveDate.Text = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, qryKYC(0).APPROVEDATE)
                        txtApproveDate.Text = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, qryKYC(0).APPROVEDATE)
                    End If
                    convertDate = Nothing

                    If Not qryKYC(0).TOTALASSET Is Nothing Then
                        clsMoney = New clsMoney
                        txtDValue.Text = clsMoney.insertComma(qryKYC(0).TOTALASSET)
                        clsMoney = Nothing
                    End If

                    If Not qryKYC(0).ISENTITYCOMP Is Nothing Then
                        rdoDEntity.SelectedIndex = qryKYC(0).ISENTITYCOMP
                    End If
                    If Not qryKYC(0).ISHOLDINGCOMP Is Nothing Then
                        rdoDHold.SelectedIndex = qryKYC(0).ISHOLDINGCOMP
                    End If
                End If



            Else
                'If Not Request.QueryString("KYC") = 4 Then
                '    ScriptManager.RegisterStartupScript(Me, Me.GetType(), "message", _
                '                         "alert('" + Resources.resMsgKYC.msgCDD + "');location.href = 'NonIndividualCDD.aspx?KYC=" & Request.QueryString("KYC") & "&TypeKYC=2&CUSTSEQ=" & CustSeq & "';", True)
                '    Exit Sub
                'End If

            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub BindingDataRef()
        Try
            Dim clsLINQToDataTable As New clsLINQToDataTable

            If ds.Tables.Contains("dsCustomerComS") Then
                ds.Tables.Remove("dsCustomerComS")
            End If

            Dim sql As String = " select c.*,p.* , " _
                                  & " nvl(C.THFIRSTNAME,C.ENFIRSTNAME) FullName " _
                                  & " from MCOMPRELATION R " _
                                  & " left outer join MCUSTOMER C on R.REFCUSTSEQ = C.CUSTSEQ " _
                                  & " left outer join MPOSITION P on R.POCODE = P.POCODE " _
                                  & " where R.CUSTSEQ = '" & CustSeq & "'"

            If ds.Tables.Contains("dsCustomerCom") Then
                ds.Tables.Remove("dsCustomerCom")
            End If

            ds.Tables.Add(clsDatabase.QueryDataSet(sql, "dsCustomerCom").Tables("dsCustomerCom").Copy)


            clsLINQToDataTable = Nothing
            grdRef.DataSource = ds.Tables("dsCustomerCom")
            grdRef.DataBind() : Me.lblCount_Peaple_Cont.Text = ds.Tables("dsCustomerCom").Rows.Count
            setGrid()


            Dim clsDropDown As New clsMasterDropDown

            If Not ds.Tables("dsCustomerCom").Rows.Count = 0 Then
                If Request.QueryString("KYC") = 1 Then
                    clsDropDown.setBizRelationToDdl(ddlDRef1)
                    clsDropDown.setBizRelationToDdl(ddlDRef2)

                    bindingDropDrownRefD()
                ElseIf Request.QueryString("KYC") = 2 Then
                    clsDropDown.setBizRelationToDdl(ddlLRef1)
                    clsDropDown.setBizRelationToDdl(ddlLRef2)
                    clsDropDown.setBizRelationToDdl(ddlLRef3)
                    clsDropDown.setBizRelationToDdl(ddlLRef4)
                    clsDropDown.setBizRelationToDdl(ddlLRef5)

                    bindingDropDrownRefL()
                ElseIf Request.QueryString("KYC") = 3 Then
                    clsDropDown.setBizRelationToDdl(ddlPRef1)
                    clsDropDown.setBizRelationToDdl(ddlPRef2)

                    bindingDropDrownRefP()
                Else
                    clsDropDown.setBizRelationToDdl(ddlDRef1)
                    clsDropDown.setBizRelationToDdl(ddlDRef2)

                    clsDropDown.setBizRelationToDdl(ddlLRef1)
                    clsDropDown.setBizRelationToDdl(ddlLRef2)
                    clsDropDown.setBizRelationToDdl(ddlLRef3)
                    clsDropDown.setBizRelationToDdl(ddlLRef4)
                    clsDropDown.setBizRelationToDdl(ddlLRef5)

                    clsDropDown.setBizRelationToDdl(ddlPRef1)
                    clsDropDown.setBizRelationToDdl(ddlPRef2)

                    bindingDropDrownRefD()
                    bindingDropDrownRefL()
                    bindingDropDrownRefP()
                End If
            End If

            clsDropDown = Nothing
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Sub bindingDropDrownRefD()
        Try
            ddlDBoard1.DataSource = ds.Tables("dsCustomerCom")
            ddlDBoard1.DataTextField = "FullName"
            ddlDBoard1.DataValueField = "FullName"
            ddlDBoard1.DataBind()
            ddlDBoard1.Items.Insert(0, "")

            ddlDBoard2.DataSource = ds.Tables("dsCustomerCom")
            ddlDBoard2.DataTextField = "FullName"
            ddlDBoard2.DataValueField = "FullName"
            ddlDBoard2.DataBind()
            ddlDBoard2.Items.Insert(0, "")

            ddlDBoard3.DataSource = ds.Tables("dsCustomerCom")
            ddlDBoard3.DataTextField = "FullName"
            ddlDBoard3.DataValueField = "FullName"
            ddlDBoard3.DataBind()
            ddlDBoard3.Items.Insert(0, "")

            ddlDBoard4.DataSource = ds.Tables("dsCustomerCom")
            ddlDBoard4.DataTextField = "FullName"
            ddlDBoard4.DataValueField = "FullName"
            ddlDBoard4.DataBind()
            ddlDBoard4.Items.Insert(0, "")
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Sub bindingDropDrownRefL()
        Try
            ddlLBoard1.DataSource = ds.Tables("dsCustomerCom")
            ddlLBoard1.DataTextField = "FullName"
            ddlLBoard1.DataValueField = "FullName"
            ddlLBoard1.DataBind()
            ddlLBoard1.Items.Insert(0, "")

            ddlLBoard2.DataSource = ds.Tables("dsCustomerCom")
            ddlLBoard2.DataTextField = "FullName"
            ddlLBoard2.DataValueField = "FullName"
            ddlLBoard2.DataBind()
            ddlLBoard2.Items.Insert(0, "")

            ddlLBoard3.DataSource = ds.Tables("dsCustomerCom")
            ddlLBoard3.DataTextField = "FullName"
            ddlLBoard3.DataValueField = "FullName"
            ddlLBoard3.DataBind()
            ddlLBoard3.Items.Insert(0, "")

            ddlLBoard4.DataSource = ds.Tables("dsCustomerCom")
            ddlLBoard4.DataTextField = "FullName"
            ddlLBoard4.DataValueField = "FullName"
            ddlLBoard4.DataBind()
            ddlLBoard4.Items.Insert(0, "")

            ddlLBoard5.DataSource = ds.Tables("dsCustomerCom")
            ddlLBoard5.DataTextField = "FullName"
            ddlLBoard5.DataValueField = "FullName"
            ddlLBoard5.DataBind()
            ddlLBoard5.Items.Insert(0, "")

            ddlLBoard6.DataSource = ds.Tables("dsCustomerCom")
            ddlLBoard6.DataTextField = "FullName"
            ddlLBoard6.DataValueField = "FullName"
            ddlLBoard6.DataBind()
            ddlLBoard6.Items.Insert(0, "")

            ddlLBoard7.DataSource = ds.Tables("dsCustomerCom")
            ddlLBoard7.DataTextField = "FullName"
            ddlLBoard7.DataValueField = "FullName"
            ddlLBoard7.DataBind()
            ddlLBoard7.Items.Insert(0, "")

            ddlLBoard8.DataSource = ds.Tables("dsCustomerCom")
            ddlLBoard8.DataTextField = "FullName"
            ddlLBoard8.DataValueField = "FullName"
            ddlLBoard8.DataBind()
            ddlLBoard8.Items.Insert(0, "")

            ddlLBoard9.DataSource = ds.Tables("dsCustomerCom")
            ddlLBoard9.DataTextField = "FullName"
            ddlLBoard9.DataValueField = "FullName"
            ddlLBoard9.DataBind()
            ddlLBoard9.Items.Insert(0, "")

            ddlLBoard10.DataSource = ds.Tables("dsCustomerCom")
            ddlLBoard10.DataTextField = "FullName"
            ddlLBoard10.DataValueField = "FullName"
            ddlLBoard10.DataBind()
            ddlLBoard10.Items.Insert(0, "")
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Sub bindingDropDrownRefP()
        Try
            ddlPBoard1.DataSource = ds.Tables("dsCustomerCom")
            ddlPBoard1.DataTextField = "FullName"
            ddlPBoard1.DataValueField = "FullName"
            ddlPBoard1.DataBind()
            ddlPBoard1.Items.Insert(0, "")

            ddlPBoard2.DataSource = ds.Tables("dsCustomerCom")
            ddlPBoard2.DataTextField = "FullName"
            ddlPBoard2.DataValueField = "FullName"
            ddlPBoard2.DataBind()
            ddlPBoard2.Items.Insert(0, "")

            ddlPBoard3.DataSource = ds.Tables("dsCustomerCom")
            ddlPBoard3.DataTextField = "FullName"
            ddlPBoard3.DataValueField = "FullName"
            ddlPBoard3.DataBind()
            ddlPBoard3.Items.Insert(0, "")

            ddlPBoard4.DataSource = ds.Tables("dsCustomerCom")
            ddlPBoard4.DataTextField = "FullName"
            ddlPBoard4.DataValueField = "FullName"
            ddlPBoard4.DataBind()
            ddlPBoard4.Items.Insert(0, "")
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

#Region "Function Drop Drown"


    Private Sub indexBizChanging2()
        If Not ddlBusiness2.SelectedValue = "" Then
            If ddlBusiness2.SelectedValue = 99 Then
                txtBizOther2.Visible = True
                lblOther22.Visible = True
            Else
                txtBizOther2.Visible = False
                txtBizOther2.Text = ""
                lblOther22.Visible = False
            End If
        End If

    End Sub

    Private Sub indexBizChanging3()

        If Not ddlBusiness3.SelectedValue = "" Then
            If ddlBusiness3.SelectedValue = 99 Then
                txtBizOther3.Visible = True
                lblOther32.Visible = True
            Else
                txtBizOther3.Visible = False
                txtBizOther3.Text = ""
                lblOther32.Visible = False
            End If
        End If

    End Sub

#End Region

    Private Function checkBeforeSave() As Boolean
        Try
            Dim result As Boolean = True

            If hdfTranType.Value = 2 Then
                'If ddlFormalProvice.SelectedIndex = 0 Then
                '    result = False
                '    Image7.ImageUrl = "~/img/icon/cancel.png"
                '    strMsg = strMsg + Resources.resMsgAlertKYC.NotAddress + "\n"
                'Else
                '    Image7.ImageUrl = "~/img/icon/accept.png"
                'End If

                'If txtFormalPhone1_1.Text = "" Then
                '    result = False
                '    Image7.ImageUrl = "~/img/icon/cancel.png"
                '    strMsg = strMsg + Resources.resMsgAlertKYC.NotPhone + "\n"
                'Else
                '    Image7.ImageUrl = "~/img/icon/accept.png"
                'End If
                '----------------------------------
                If txtFormalAddress.Text = "" Then
                    result = False
                    Image7.ImageUrl = "~/img/icon/cancel.png"
                    strMsg = strMsg + Resources.resMsgAlertKYC.NotAddressNo + "\n"
                Else
                    Image7.ImageUrl = "~/img/icon/accept.png"
                End If

                If ddlFormalProvice.SelectedIndex = 0 Then
                    result = False
                    Image7.ImageUrl = "~/img/icon/cancel.png"
                    strMsg = strMsg + Resources.resMsgAlertKYC.NotProvice + "\n"
                Else
                    Image7.ImageUrl = "~/img/icon/accept.png"
                End If

                If ddlFormalAmphor.SelectedIndex = 0 Then
                    result = False
                    Image7.ImageUrl = "~/img/icon/cancel.png"
                    strMsg = strMsg + Resources.resMsgAlertKYC.NotAmphor + "\n"
                Else
                    Image7.ImageUrl = "~/img/icon/accept.png"
                End If

                If ddlFormalTambol.SelectedIndex = 0 Then
                    result = False
                    Image7.ImageUrl = "~/img/icon/cancel.png"
                    strMsg = strMsg + Resources.resMsgAlertKYC.NotTambol + "\n"
                Else
                    Image7.ImageUrl = "~/img/icon/accept.png"
                End If

                If txtFormalPostCode.Text = "" Then
                    result = False
                    Image7.ImageUrl = "~/img/icon/cancel.png"
                    strMsg = strMsg + Resources.resMsgAlertKYC.notPostCode + "\n"
                Else
                    Image7.ImageUrl = "~/img/icon/accept.png"
                End If
            End If



            If ddlCheckName.SelectedIndex = 0 Then
                result = False
                Image1.ImageUrl = "~/img/icon/cancel.png"
                strMsg = strMsg + Resources.resMsgAlertKYC.NotCheckAddress + "\n"
            Else
                Image1.ImageUrl = "~/img/icon/accept.png"
            End If


            Dim cls As New clsAccount
            If Request.QueryString("KYC") = 1 Then

                If Not cls.chkAccountDisposit(TxtAccNo.Text.Trim) Then
                    strMsg = strMsg + cls.SetMsg + "\n"
                    result = False
                End If

            ElseIf Request.QueryString("KYC") = 2 Then
                If Not cls.chkAccountLoan(TxtAccNo.Text.Trim) Then
                    strMsg = strMsg + cls.SetMsg + "\n"
                    result = False
                End If
            End If
            If Request.QueryString("KYC") = 1 Or Request.QueryString("KYC") = 3 Then
                If txtAccName.Text = "" Then
                    strMsg = strMsg + "กรุณากรอกชื่อบัญชี" + "\n"
                    result = False
                Else
                    If Not IsNumeric(TxtAccNo.Text) Then
                        strMsg = strMsg + "เลขที่บัญชีต้องเป็นตัวเลข" + "\n"
                        result = False
                    End If
                End If
            End If



            If PanelDepositSactionC.Visible = True Then

                If chkDAsset5.Checked = True Then
                    If txtDAssetOther.Text = "" Then
                        result = False
                        Image1.ImageUrl = "~/img/icon/cancel.png"
                        strMsg = strMsg + Resources.resMsgAlertKYC.NotOther + "\n"
                    Else
                        Image1.ImageUrl = "~/img/icon/accept.png"
                    End If
                End If

            End If



            If Request.QueryString("KYC") = 1 Then
                If chkDAcc4.Checked = True And txtDAccOther.Text.Trim = "" Then
                    result = False
                    Image2.ImageUrl = "~/img/icon/cancel.png"
                    strMsg = strMsg + Resources.resMsgAlertKYC.NotOther + "\n"
                Else
                    Image2.ImageUrl = "~/img/icon/accept.png"
                End If
                If chkDSource5.Checked = True And txtDSourceOther.Text.Trim = "" Then
                    result = False
                    Image3.ImageUrl = "~/img/icon/cancel.png"
                    strMsg = strMsg + Resources.resMsgAlertKYC.NotOther + "\n"
                Else
                    Image3.ImageUrl = "~/img/icon/accept.png"
                End If

            ElseIf Request.QueryString("KYC") = 2 Then
                If chkLAcc7.Checked = True And txtLAccOther.Text.Trim = "" Then
                    result = False
                    strMsg = strMsg + Resources.resMsgAlertKYC.NotOther + "\n"
                Else

                End If
            ElseIf Request.QueryString("KYC") = 3 Then
                If chkPAcc4.Checked = True And txtPAccOther.Text.Trim = "" Then
                    result = False
                    strMsg = strMsg + Resources.resMsgAlertKYC.NotOther + "\n"
                Else
                End If
                If chkPSource5.Checked = True And txtPSourceOther.Text.Trim = "" Then
                    result = False

                    strMsg = strMsg + Resources.resMsgAlertKYC.NotOther + "\n"
                Else

                End If
            End If

            If result = False Then
                statusValidation()
            End If

            Return result
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Private Function checkNPASave() As Boolean
        Try
            Dim result As Boolean = True

            If hdfTranType.Value = 2 Then

                '----------------------------------
                If txtFormalAddress.Text = "" Then
                    result = False
                    Image7.ImageUrl = "~/img/icon/cancel.png"
                    strMsg = strMsg + Resources.resMsgAlertKYC.NotAddressNo + "\n"
                Else
                    Image7.ImageUrl = "~/img/icon/accept.png"
                End If

                If ddlFormalProvice.SelectedIndex = 0 Then
                    result = False
                    Image7.ImageUrl = "~/img/icon/cancel.png"
                    strMsg = strMsg + Resources.resMsgAlertKYC.NotProvice + "\n"
                Else
                    Image7.ImageUrl = "~/img/icon/accept.png"
                End If

                If ddlFormalAmphor.SelectedIndex = 0 Then
                    result = False
                    Image7.ImageUrl = "~/img/icon/cancel.png"
                    strMsg = strMsg + Resources.resMsgAlertKYC.NotAmphor + "\n"
                Else
                    Image7.ImageUrl = "~/img/icon/accept.png"
                End If

                If ddlFormalTambol.SelectedIndex = 0 Then
                    result = False
                    Image7.ImageUrl = "~/img/icon/cancel.png"
                    strMsg = strMsg + Resources.resMsgAlertKYC.NotTambol + "\n"
                Else
                    Image7.ImageUrl = "~/img/icon/accept.png"
                End If

                If txtFormalPostCode.Text = "" Then
                    result = False
                    Image7.ImageUrl = "~/img/icon/cancel.png"
                    strMsg = strMsg + Resources.resMsgAlertKYC.notPostCode + "\n"
                Else
                    Image7.ImageUrl = "~/img/icon/accept.png"
                End If
            End If





            If ddlCheckName.SelectedIndex = 0 Then
                result = False
                Image1.ImageUrl = "~/img/icon/cancel.png"
                strMsg = strMsg + Resources.resMsgAlertKYC.NotCheckAddress + "\n"
            Else
                Image1.ImageUrl = "~/img/icon/accept.png"
            End If


            If result = False Then
                statusValidation()
            End If

            Return result
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Sub statusValidation()

        Image1.Visible = True
        Image2.Visible = True
        Image3.Visible = True
    End Sub

    Private Sub saveData()

        KYCEntity = New KYCEntities
        KYCEntity.Connection.Open()
        Using transaction As System.Data.Common.DbTransaction = KYCEntity.Connection.BeginTransaction()
            Try
                saveCustomer()

                ''''Prachak '''''''''''''''''''''''''''
                Me.Save_to_MCUST_BO_Deposit(CustSeq, Me.TxtAccNo.Text)

                '''''Deposit'''''''''''''''''''''''''''
                Me.Save_to_MCUST_OBJ_Deposit(CustSeq, Me.TxtAccNo.Text)
                Me.Save_to_MCUST_SOURCE_Deposit(CustSeq, Me.TxtAccNo.Text)

                '' '''''Loan ''''''''''''''''''''''''''''''
                Me.Save_to_MCUST_OBJ_Loan(CustSeq, Me.TxtAccNo.Text)

                '' '''''Promisary Note ''''''''''''''''''''''''''''''
                Me.Save_to_MCUST_OBJ_PN(CustSeq, Me.TxtAccNo.Text)
                Me.Save_to_MCUST_SOURCE_PN(CustSeq, Me.TxtAccNo.Text)


                ''''Prachak '''''''''''''''''''
                If Request.QueryString("KYC") = 1 Then
                    If Me.lblKYCSEQ.Text = "-1" Then
                        Me.saveNewKYCDeposit()
                    Else
                        Me.saveUpdateKYCDeposit()
                    End If

                ElseIf Request.QueryString("KYC") = 2 Then
                    If Me.lblKYCSEQ.Text = "-1" Then
                        saveNewKYCLoan()
                    Else
                        saveUpdateKYCLoan()
                    End If

                ElseIf Request.QueryString("KYC") = 3 Then
                    If Me.lblKYCSEQ.Text = "-1" Then
                        saveNewKYCPN()
                    Else
                        saveUpdateKYCPN()
                    End If
                End If


                '''''''''
                Me.grdShowAccount.SelectedIndex = -1
                Me.btnSave.Visible = False




                ' ''If (ds.Tables("rkyc").Rows.Count > 0) Then
                ' ''    If Request.QueryString("KYC") = 4 Then
                ' ''        If statusTran(1) = 0 Then 'New
                ' ''            saveNewKYCDeposit()
                ' ''        Else
                ' ''            saveUpdateKYCDeposit()

                ' ''        End If
                ' ''        If statusTran(2) = 0 Then 'New
                ' ''            saveNewKYCLoan()
                ' ''        Else
                ' ''            saveUpdateKYCLoan()

                ' ''        End If
                ' ''        If statusTran(3) = 0 Then 'New
                ' ''            saveNewKYCPN()
                ' ''        Else
                ' ''            saveUpdateKYCPN()

                ' ''        End If
                ' ''    Else
                ' ''        If statusTran(CInt(Request.QueryString("KYC"))) = 0 Then 'New
                ' ''            If Request.QueryString("KYC") = 1 Then
                ' ''                saveNewKYCDeposit()
                ' ''            ElseIf Request.QueryString("KYC") = 2 Then
                ' ''                saveNewKYCLoan()
                ' ''            ElseIf Request.QueryString("KYC") = 3 Then
                ' ''                saveNewKYCPN()
                ' ''            End If

                ' ''        Else 'Edit
                ' ''            If (ds.Tables("rkyc").Rows.Count > 0) Then
                ' ''                If Request.QueryString("KYC") = 1 Then
                ' ''                    saveUpdateKYCDeposit()
                ' ''                ElseIf Request.QueryString("KYC") = 2 Then
                ' ''                    saveUpdateKYCLoan()
                ' ''                ElseIf Request.QueryString("KYC") = 3 Then
                ' ''                    saveUpdateKYCPN()
                ' ''                End If
                ' ''            End If


                ' ''        End If
                ' ''    End If

                ' ''End If

                transaction.Commit()
            Catch ex As Exception
                Throw ex
            End Try
        End Using

        KYCEntity.SaveChanges()
        KYCEntity = Nothing

    End Sub

    Private Sub saveNPA()



        KYCEntity = New KYCEntities
        KYCEntity.Connection.Open()
        Using transaction As System.Data.Common.DbTransaction = KYCEntity.Connection.BeginTransaction()
            Try
                saveCustomer()

                transaction.Commit()
            Catch ex As Exception
                Throw ex
            End Try
        End Using

        KYCEntity.SaveChanges()
        KYCEntity = Nothing

    End Sub

    Private Function statusTran(ByVal KYC As Integer) As Integer
        Try
            'Dim KYC As Integer = CInt(Request.QueryString("KYC"))
            If KYC = 1 Then
                KYC = 2
            ElseIf KYC = 2 Then
                KYC = 4
            ElseIf KYC = 3 Then
                KYC = 6
            End If

            Dim tran As Integer
            Dim qryK = (From k In KYCEntity.RKYC
                         Where k.CUSTSEQ = CustSeq And k.ANSWERTYPE = KYC
                         Select k.CUSTSEQ).ToList

            Dim i As Integer = qryK.Count
            If i = 0 Then
                tran = 0 'New
            Else
                tran = 1 'Edit
            End If

            Return tran
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Private Sub saveCustomer()
        Dim strTempProvince As String = ""
        Dim strTempTambol As String = ""
        Dim strTempAmphor As String = ""

        Dim updateCust = (From Cust In KYCEntity.MCUSTOMER
                        Where Cust.CUSTSEQ = CustSeq
                        Select Cust).First()

        If hdfTranType.Value = 2 Then
            'updateCust.FORMALADDR = txtFormalAddr.Text
            'updateCust.FORMALPHONE1 = txtFormalPhone1.Text
            'updateCust.FORMALPHONE2 = txtFormalPhone2.Text
            'updateCust.FORMALPHONE3 = txtFormalPhone3.Text
            'updateCust.FORMALFAX = txtFormalFax.Text
            updateCust.FORMALNO = txtFormalAddress.Text.Trim
            updateCust.FORMALMOO = txtFormalMoo.Text.Trim
            updateCust.FORMALSTREET = txtFormalStreet.Text.Trim
            updateCust.FORMALSOIDESC = txtFormalSoi.Text.Trim
            updateCust.FORMALBUILDING = txtFormalBuilding.Text.Trim
            updateCust.FORMALPROVINCE = ddlFormalProvice.SelectedValue
            updateCust.FORMALAMPHOR = ddlFormalAmphor.SelectedValue
            updateCust.FORMALTAMBOL = ddlFormalTambol.SelectedValue
            updateCust.FORMALPHONE1 = txtFormalPhone1_1.Text.Trim
            updateCust.FORMALPHONE2 = txtFormalPhone2_2.Text.Trim
            updateCust.FORMALPHONE3 = txtFormalPhone3_3.Text.Trim
            updateCust.FORMALFAX = txtFormalFax1_1.Text.Trim
            updateCust.FORMALZIP = txtFormalPostCode.Text.Trim

            If ddlFormalTambol.Items.Count = 0 Then
                strTempTambol = "-"
            Else
                If (ddlFormalTambol.SelectedIndex = 0) Then
                    strTempTambol = "-"
                Else
                    strTempTambol = ddlFormalTambol.SelectedItem.ToString()
                End If
            End If

            If ddlFormalAmphor.Items.Count = 0 Then
                strTempAmphor = "-"
            Else

                If (ddlFormalAmphor.SelectedIndex = 0) Then
                    strTempAmphor = "-"
                Else
                    strTempAmphor = ddlFormalAmphor.SelectedItem.ToString()
                End If
            End If

            If ddlFormalProvice.Items.Count = 0 Then
                strTempProvince = "-"
            Else
                If (ddlFormalProvice.SelectedIndex = 0) Then
                    strTempProvince = "-"
                Else
                    strTempProvince = ddlFormalProvice.SelectedItem.ToString()
                End If
            End If

            updateCust.FORMALADDR = concatAddress(txtFormalAddress.Text, txtFormalMoo.Text, _
                                               txtFormalSoi.Text, txtFormalStreet.Text, _
                                                strTempTambol, _
                                                strTempAmphor, _
                                                strTempProvince, _
                                               txtFormalPostCode.Text)

        End If

        updateCust.CHECKADDRESS = ddlCheckName.SelectedValue

        If strCdd = "1" Then
            If Request.QueryString("KYC") = 1 Then
                updateCust.DEPUPDATE = "D"

                Dim intCountComplete As Integer = 0

                If (grdShowAccount.SelectedIndex > -1) Then

                    Dim strKYCSEQ As String = CType(grdShowAccount.Rows(grdShowAccount.SelectedIndex).FindControl("lblKYCSeq"), Label).Text

                    For i As Integer = 0 To ds.Tables("rkyc").Rows.Count - 1
                        If Not strKYCSEQ = ds.Tables("rkyc").Rows(i).Item("KYCSEQ").ToString Then
                            If ds.Tables("rkyc").Rows(i).Item("KYCCOMPLETE").ToString.Trim = "N" Then
                                intCountComplete += 1
                            End If
                        End If
                    Next

                End If
                If intCountComplete = 0 Then
                    If chkDataKYC(1) Then
                        updateCust.DEPD = "Y"
                    Else
                        If (ds.Tables("rkyc").Rows.Count = 0) Then
                            updateCust.DEPD = "Y"
                        Else
                            updateCust.DEPD = "N"
                        End If

                    End If

                Else
                    updateCust.DEPD = "N"
                End If

                ' updateCust.DEPD = "Y"
            ElseIf Request.QueryString("KYC") = 2 Then
                updateCust.DEPUPDATE = "L"
                Dim intCountComplete As Integer = 0

                If (grdShowAccount.SelectedIndex > -1) Then

                    Dim strKYCSEQ As String = CType(grdShowAccount.Rows(grdShowAccount.SelectedIndex).FindControl("lblKYCSeq"), Label).Text

                    For i As Integer = 0 To ds.Tables("rkyc").Rows.Count - 1
                        If Not strKYCSEQ = ds.Tables("rkyc").Rows(i).Item("KYCSEQ").ToString Then
                            If ds.Tables("rkyc").Rows(i).Item("KYCCOMPLETE").ToString.Trim = "N" Then
                                intCountComplete += 1
                            End If
                        End If
                    Next

                End If

                If intCountComplete = 0 Then
                    If chkDataKYC(2) Then
                        updateCust.DEPL = "Y"
                    Else
                        If (grdShowAccount.SelectedIndex > -1) Then
                            updateCust.DEPL = "N"
                        Else
                            updateCust.DEPL = "Y"
                        End If

                    End If

                Else
                    updateCust.DEPL = "N"
                End If
                '   updateCust.DEPL = "Y"
            ElseIf Request.QueryString("KYC") = 3 Then
                updateCust.DEPUPDATE = "P"
                Dim intCountComplete As Integer = 0

                If (grdShowAccount.SelectedIndex > -1) Then

                    Dim strKYCSEQ As String = CType(grdShowAccount.Rows(grdShowAccount.SelectedIndex).FindControl("lblKYCSeq"), Label).Text

                    For i As Integer = 0 To ds.Tables("rkyc").Rows.Count - 1
                        If Not strKYCSEQ = ds.Tables("rkyc").Rows(i).Item("KYCSEQ").ToString Then
                            If ds.Tables("rkyc").Rows(i).Item("KYCCOMPLETE").ToString.Trim = "N" Then
                                intCountComplete += 1
                            End If
                        End If
                    Next

                End If
                If intCountComplete = 0 Then
                    If chkDataKYC(3) Then
                        updateCust.DEPP = "Y"
                    Else
                        If (ds.Tables("rkyc").Rows.Count = 0) Then
                            updateCust.DEPP = "Y"
                        Else
                            updateCust.DEPP = "N"
                        End If

                    End If

                Else
                    updateCust.DEPP = "N"
                End If
                'updateCust.DEPP = "Y"
            ElseIf Request.QueryString("KYC") = 5 Then
                updateCust.DEPUPDATE = "N"
                updateCust.DEPN = "Y"

            End If
        Else
            If Request.QueryString("KYC") = 1 Then
                updateCust.DEPUPDATE = "D"
                Dim intCountComplete As Integer = 0
                Dim strKYCSEQ As String = ""
                If (grdShowAccount.SelectedIndex > -1) Then
                    strKYCSEQ = CType(grdShowAccount.Rows(grdShowAccount.SelectedIndex).FindControl("lblKYCSeq"), Label).Text
                    For i As Integer = 0 To ds.Tables("rkyc").Rows.Count - 1
                        If Not strKYCSEQ = ds.Tables("rkyc").Rows(i).Item("KYCSEQ").ToString Then
                            If ds.Tables("rkyc").Rows(i).Item("KYCCOMPLETE").ToString.Trim = "N" Then
                                intCountComplete += 1
                            End If
                        End If
                    Next
                End If


                If intCountComplete = 0 Then
                    If chkDataKYC(1) Then
                        updateCust.DEPD = "Y"
                    Else
                        If (ds.Tables("rkyc").Rows.Count = 0) Then
                            updateCust.DEPD = "Y"
                        Else
                            updateCust.DEPD = "N"
                        End If

                    End If

                Else
                    updateCust.DEPD = "N"
                End If
                ' updateCust.DEPD = "Y"
                'End If
            ElseIf Request.QueryString("KYC") = 2 Then
                updateCust.DEPUPDATE = "L"
                'If ddlDApprove.SelectedValue = 1 Then
                '    updateCust.DEPL = "N"
                'Else
                Dim strKYCSEQ As String = ""
                If (grdShowAccount.SelectedIndex > -1) Then
                    strKYCSEQ = CType(grdShowAccount.Rows(grdShowAccount.SelectedIndex).FindControl("lblKYCSeq"), Label).Text
                End If

                Dim intCountComplete As Integer = 0
                For i As Integer = 0 To ds.Tables("rkyc").Rows.Count - 1
                    If Not strKYCSEQ = ds.Tables("rkyc").Rows(i).Item("KYCSEQ").ToString Then
                        If ds.Tables("rkyc").Rows(i).Item("KYCCOMPLETE").ToString.Trim = "N" Then
                            intCountComplete += 1
                        End If
                    End If
                Next
                If intCountComplete = 0 Then

                    If chkDataKYC(2) Then
                        updateCust.DEPL = "Y"
                    Else

                        If (grdShowAccount.SelectedIndex > -1) Then
                            updateCust.DEPL = "N"
                        Else
                            updateCust.DEPL = "Y"
                        End If

                    End If
                Else
                    updateCust.DEPL = "N"
                End If
                ' updateCust.DEPL = "Y"
                'End If
            ElseIf Request.QueryString("KYC") = 3 Then
                updateCust.DEPUPDATE = "P"
                Dim intCountComplete As Integer = 0
                Dim strKYCSEQ As String = ""
                If (grdShowAccount.SelectedIndex > -1) Then
                    strKYCSEQ = CType(grdShowAccount.Rows(grdShowAccount.SelectedIndex).FindControl("lblKYCSeq"), Label).Text
                    For i As Integer = 0 To ds.Tables("rkyc").Rows.Count - 1
                        If Not strKYCSEQ = ds.Tables("rkyc").Rows(i).Item("KYCSEQ").ToString Then
                            If ds.Tables("rkyc").Rows(i).Item("KYCCOMPLETE").ToString.Trim = "N" Then
                                intCountComplete += 1
                            End If
                        End If
                    Next

                End If


                If intCountComplete = 0 Then
                    If chkDataKYC(3) Then
                        updateCust.DEPP = "Y"
                    Else
                        If (ds.Tables("rkyc").Rows.Count = 0) Then
                            updateCust.DEPP = "Y"
                        Else
                            updateCust.DEPP = "N"
                        End If
                    End If
                Else
                    updateCust.DEPP = "N"
                End If
                '  updateCust.DEPP = "Y"
                'End If
            ElseIf Request.QueryString("KYC") = 5 Then
                updateCust.DEPUPDATE = "N"
                updateCust.DEPN = "Y"
            End If
        End If
        If PanelDepositSactionC.Visible = True Then
            ' updateCust.APPROVECODE = ddlDApprove.SelectedValue
            updateCust.APPROVECODE = 0
        End If
        'updateCust.UPDATEBY = clsSessionMenuRight.getUserName
        'updateCust.UPDATEDATE = Now.ToShortDateString
    End Sub

    Private Sub saveUpdateKYCDeposit()

        Dim lvsCustType As Integer = Session("KYC")

        Dim intKycSeq As Integer = hdfKYCSeq.Value
        Dim updateKYC = (From k In KYCEntity.RKYC
                       Where k.CUSTSEQ = CustSeq And k.KYCSEQ = intKycSeq And k.ANSWERTYPE = 2
                       Select k).First()

        'updateKYC.CHECKADDRESS = ddlCheckAddress.SelectedValue
        updateKYC.ACCOUNTNO = TxtAccNo.Text.Trim
        updateKYC.ACCOUNTNAME = txtAccName.Text.Trim
        updateKYC.WORKERSTATUS = "0"

        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        '''''Prachak '''''''''''''''''''''''''''''''''''''''''''''
        updateKYC.ACCT_STATE = Me.lblACCT_STATE.Text
        '''''''Self BO'''''''''''''''
        If chkBO_SELF.Checked = True Then
            updateKYC.FLAG_SELF_BO = "Y"
        Else
            updateKYC.FLAG_SELF_BO = "N"
        End If
        ''''BO Other ''''''''''''''''''
        If chkBO_OTH.Checked = True Then
            updateKYC.FLAG_OTHP_BO = "Y"
        Else
            updateKYC.FLAG_OTHP_BO = "N"
        End If
        ''''Active'''''''''''''''''''''''
        updateKYC.ACTIVE = Me.lblACTIVE.Text
        updateKYC.CUSTTYPE = lvsCustType
        updateKYC.F_JOIN_ACCT = "S"
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

        Dim intAns1 As Integer = 0
        Dim intAns2 As Integer = 0

        If chkDAcc1.Checked = True Then
            updateKYC.ANSWER101 = 0
            intAns1 += 1
        Else
            updateKYC.ANSWER101 = 1
        End If
        If chkDAcc2.Checked = True Then
            updateKYC.ANSWER102 = 0
            intAns1 += 1
        Else
            updateKYC.ANSWER102 = 1
        End If
        If chkDAcc3.Checked = True Then
            updateKYC.ANSWER103 = 0
            intAns1 += 1
        Else
            updateKYC.ANSWER103 = 1
        End If
        If chkDAcc4.Checked = True Then
            updateKYC.ANSWER104 = 0
            intAns1 += 1
            updateKYC.ANSWER199 = txtDAccOther.Text.Trim
        Else
            updateKYC.ANSWER104 = 1
        End If

        If chkDSource1.Checked = True Then
            updateKYC.ANSWER201 = 0
            intAns2 += 1
        Else
            updateKYC.ANSWER201 = 1
        End If
        If chkDSource2.Checked = True Then
            updateKYC.ANSWER202 = 0
            intAns2 += 1
        Else
            updateKYC.ANSWER202 = 1
        End If
        If chkDSource3.Checked = True Then
            updateKYC.ANSWER203 = 0
            intAns2 += 1
        Else
            updateKYC.ANSWER203 = 1
        End If
        If chkDSource4.Checked = True Then
            updateKYC.ANSWER204 = 0
            intAns2 += 1
        Else
            updateKYC.ANSWER204 = 1
        End If
        If chkDSource5.Checked = True Then
            updateKYC.ANSWER205 = 0
            intAns2 += 1
            updateKYC.ANSWER299 = txtDSourceOther.Text.Trim
        Else
            updateKYC.ANSWER205 = 1
        End If

        If rdoDRef.Items(0).Selected = True Then 'มีความสัมพันธ์
            updateKYC.ANSWER301 = 0
        Else
            updateKYC.ANSWER301 = 1
        End If

        updateKYC.REF101 = ddlDBoard1.SelectedValue
        updateKYC.REF102 = ddlDBoard2.SelectedValue
        updateKYC.REF103 = ddlDRef1.SelectedValue
        updateKYC.REF104 = txtDRef1.Text.Trim
        updateKYC.REF201 = ddlDBoard3.SelectedValue
        updateKYC.REF202 = ddlDBoard4.SelectedValue
        updateKYC.REF203 = ddlDRef2.SelectedValue
        updateKYC.REF204 = txtDRef2.Text.Trim

        updateKYC.ANSWER401 = ddlDDeposit.SelectedValue
        updateKYC.ANSWER501 = ddlDMoney.SelectedValue

        If PanelDepositSactionC.Visible = True Then
            If chkDAsset1.Checked = True Then
                updateKYC.ASSET1 = 0
            Else
                updateKYC.ASSET1 = 1
            End If
            If chkDAsset2.Checked = True Then
                updateKYC.ASSET2 = 0
            Else
                updateKYC.ASSET2 = 1
            End If
            If chkDAsset3.Checked = True Then
                updateKYC.ASSET3 = 0
            Else
                updateKYC.ASSET3 = 1
            End If
            If chkDAsset4.Checked = True Then
                updateKYC.ASSET4 = 0
            Else
                updateKYC.ASSET4 = 1
            End If
            If chkDAsset5.Checked = True Then
                updateKYC.ASSET5 = 0
                updateKYC.ASSETOTHER = txtDAssetOther.Text
            Else
                updateKYC.ASSET5 = 1
            End If


            If txtDValue.Text = "" Then
                updateKYC.TOTALASSET = Nothing
            Else
                clsMoney = New clsMoney
                updateKYC.TOTALASSET = clsMoney.deleteComma(txtDValue.Text.Trim)
                clsMoney = Nothing
            End If
            updateKYC.ISENTITYCOMP = rdoDEntity.SelectedIndex
            updateKYC.ISHOLDINGCOMP = rdoDHold.SelectedIndex


            updateKYC.APPROVECODE = ddlDApprove.SelectedValue
            updateKYC.APPROVENAME = txtDApproveNo.Text.Trim
            updateKYC.APPROVENO = txtDApproveNo.Text.Trim
            If txtDApproveDate.Text = "" Then
                updateKYC.APPROVEDATE = Nothing
            Else
                Dim clsFormatDate As clsDateFormat = New clsDateFormat
                updateKYC.APPROVEDATE = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDApproveDate.Text)
                clsFormatDate = Nothing
            End If

            'If ddlDApprove.SelectedValue = 1 Then
            '    updateKYC.KYCCOMPLETE = "N"
            'Else
            If intAns1 = 0 Or intAns2 = 0 Or ddlDDeposit.SelectedIndex = 0 Or ddlDMoney.SelectedIndex = 0 Then
                updateKYC.KYCCOMPLETE = "N"
            Else
                updateKYC.KYCCOMPLETE = "Y" 'ถือว่าทำ KYC สมบูรณ์แล้ว
            End If



            'End If
        Else
            'If strCdd = "1" Then
            If intAns1 = 0 Or intAns2 = 0 Or ddlDDeposit.SelectedIndex = 0 Or ddlDMoney.SelectedIndex = 0 Then
                updateKYC.KYCCOMPLETE = "N"
            Else
                updateKYC.KYCCOMPLETE = "Y"
            End If


            'Else
            '    updateKYC.KYCCOMPLETE = ""
            'End If
        End If

        'updateKYC.KYCCOMPLETE = "Y"

        If Request.QueryString("KYC") = 1 Then
            updateKYC.DEPUPDATE = "D"
        ElseIf Request.QueryString("KYC") = 2 Then
            updateKYC.DEPUPDATE = "L"
        ElseIf Request.QueryString("KYC") = 3 Then
            updateKYC.DEPUPDATE = "P"

        End If
        updateKYC.BRANCHCODE = clsSessionMenuRight.getBranchCode
        updateKYC.ZONECODE = clsSessionMenuRight.getZoneCode
        updateKYC.DEPTCODE = clsSessionMenuRight.getDeptCode
        updateKYC.ALLCODERIGHT = clsSessionMenuRight.getAllCodeRight

        updateKYC.UPDATEBY = clsSessionMenuRight.getUserName
        updateKYC.UPDATEDATE = Now.ToShortDateString


    End Sub

    Private Sub saveNewKYCDeposit()
        Dim lvsCustType As Integer = Session("KYC")

        If ds.Tables.Contains("GENKYC") Then
            ds.Tables.Remove("GENKYC")
        End If

        Dim sql As String = "select " & System.Configuration.ConfigurationSettings.AppSettings("SequentRKYC_S") & " from dual"
        ds.Tables.Add(clsDatabase.QueryDataSet(sql, "GENKYC").Tables("GENKYC").Copy)
        Dim key As Integer = ds.Tables("GENKYC").Rows(0)("NEXTVAL")

        Dim newKYC As New RKYC
        newKYC.KYCSEQ = key
        newKYC.CUSTSEQ = CustSeq
        newKYC.KYCDATE = Now
        newKYC.ANSWERTYPE = 2
        newKYC.TRANSTYPE = 2

        newKYC.ACCOUNTNO = TxtAccNo.Text.Trim
        newKYC.ACCOUNTNAME = txtAccName.Text.Trim
        newKYC.WORKERSTATUS = "0"

        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        '''''Prachak '''''''''''''''''''''''''''''''''''''''''''''
        newKYC.ACCT_STATE = Me.lblACCT_STATE.Text
        '''''''Self BO'''''''''''''''
        If chkBO_SELF.Checked = True Then
            newKYC.FLAG_SELF_BO = "Y"
        Else
            newKYC.FLAG_SELF_BO = "N"
        End If
        ''''BO Other ''''''''''''''''''
        If chkBO_OTH.Checked = True Then
            newKYC.FLAG_OTHP_BO = "Y"
        Else
            newKYC.FLAG_OTHP_BO = "N"
        End If
        ''''Active'''''''''''''''''''''''
        newKYC.ACTIVE = Me.lblACTIVE.Text
        ''''CustType'''''''''''''''''''''
        newKYC.CUSTTYPE = lvsCustType
        newKYC.F_JOIN_ACCT = "S"
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        '  newKYC.KYCCOMPLETE = "Y"


        If Request.QueryString("KYC") = 1 Then
            newKYC.DEPUPDATE = "D"
        ElseIf Request.QueryString("KYC") = 2 Then
            newKYC.DEPUPDATE = "L"
        ElseIf Request.QueryString("KYC") = 3 Then
            newKYC.DEPUPDATE = "P"
        End If
        newKYC.BRANCHCODE = clsSessionMenuRight.getBranchCode
        newKYC.ZONECODE = clsSessionMenuRight.getZoneCode
        newKYC.DEPTCODE = clsSessionMenuRight.getDeptCode
        newKYC.ALLCODERIGHT = clsSessionMenuRight.getAllCodeRight
        newKYC.UPDATEBY = clsSessionMenuRight.getUserName
        newKYC.UPDATEDATE = Now.ToShortDateString

        KYCEntity.AddToRKYC(newKYC)

    End Sub

    Private Sub saveUpdateKYCLoan()
        Dim intKycSeq As Integer = hdfKYCSeq.Value
        Dim updateKYC = (From k In KYCEntity.RKYC
                       Where k.CUSTSEQ = CustSeq And k.KYCSEQ = intKycSeq And k.ANSWERTYPE = 4
                       Select k).First()

        'updateKYC.CHECKADDRESS = ddlCheckAddress.SelectedValue
        updateKYC.ACCOUNTNO = TxtAccNo.Text.Trim
        updateKYC.ACCOUNTNAME = txtAccName.Text.Trim
        updateKYC.WORKERSTATUS = "0"

        ''''Prachak''''''''''''''''''''''''''''''''''
        updateKYC.ACCT_STATE = Me.lblACCT_STATE.Text

        Dim intAns1 As Integer = 0
        If chkLAcc1.Checked = True Then
            updateKYC.ANSWER101 = 0
            intAns1 += 1
        Else
            updateKYC.ANSWER101 = 1
        End If
        If chkLAcc2.Checked = True Then
            updateKYC.ANSWER102 = 0
            intAns1 += 1
        Else
            updateKYC.ANSWER102 = 1
        End If
        If chkLAcc3.Checked = True Then
            updateKYC.ANSWER103 = 0
            intAns1 += 1
        Else
            updateKYC.ANSWER103 = 1
        End If
        If chkLAcc4.Checked = True Then
            updateKYC.ANSWER104 = 0
            intAns1 += 1
        Else
            updateKYC.ANSWER104 = 1
        End If
        If chkLAcc5.Checked = True Then
            updateKYC.ANSWER105 = 0
            intAns1 += 1
        Else
            updateKYC.ANSWER105 = 1
        End If
        If chkLAcc6.Checked = True Then
            updateKYC.ANSWER106 = 0
            intAns1 += 1
        Else
            updateKYC.ANSWER106 = 1
        End If
        If chkLAcc7.Checked = True Then
            updateKYC.ANSWER107 = 0
            intAns1 += 1
            updateKYC.ANSWER199 = txtLAccOther.Text.Trim
        Else
            updateKYC.ANSWER107 = 1
        End If

        updateKYC.REF101 = ddlLBoard1.SelectedValue
        updateKYC.REF102 = ddlLBoard2.SelectedValue
        updateKYC.REF103 = ddlLRef1.SelectedValue
        updateKYC.REF104 = txtLRef1.Text.Trim
        updateKYC.REF201 = ddlLBoard3.SelectedValue
        updateKYC.REF202 = ddlLBoard4.SelectedValue
        updateKYC.REF203 = ddlLRef2.SelectedValue
        updateKYC.REF204 = txtLRef2.Text.Trim
        updateKYC.REF301 = ddlLBoard5.SelectedValue
        updateKYC.REF302 = ddlLBoard6.SelectedValue
        updateKYC.REF303 = ddlLRef3.SelectedValue
        updateKYC.REF304 = txtLRef3.Text.Trim
        updateKYC.REF401 = ddlLBoard7.SelectedValue
        updateKYC.REF402 = ddlLBoard8.SelectedValue
        updateKYC.REF403 = ddlLRef4.SelectedValue
        updateKYC.REF404 = txtLRef4.Text.Trim
        updateKYC.REF501 = ddlLBoard9.SelectedValue
        updateKYC.REF502 = ddlLBoard10.SelectedValue
        updateKYC.REF503 = ddlLRef5.SelectedValue
        updateKYC.REF504 = txtLRef5.Text.Trim

        If PanelDepositSactionC.Visible = True Then
            If chkDAsset1.Checked = True Then
                updateKYC.ASSET1 = 0
            Else
                updateKYC.ASSET1 = 1
            End If
            If chkDAsset2.Checked = True Then
                updateKYC.ASSET2 = 0
            Else
                updateKYC.ASSET2 = 1
            End If
            If chkDAsset3.Checked = True Then
                updateKYC.ASSET3 = 0
            Else
                updateKYC.ASSET3 = 1
            End If
            If chkDAsset4.Checked = True Then
                updateKYC.ASSET4 = 0
            Else
                updateKYC.ASSET4 = 1
            End If
            If chkDAsset5.Checked = True Then
                updateKYC.ASSET5 = 0
                updateKYC.ASSETOTHER = txtDAssetOther.Text
            Else
                updateKYC.ASSET5 = 1
            End If

            If txtDValue.Text = "" Then
                updateKYC.TOTALASSET = Nothing
            Else
                clsMoney = New clsMoney
                updateKYC.TOTALASSET = clsMoney.deleteComma(txtDValue.Text.Trim)
                clsMoney = Nothing
            End If

            updateKYC.ISENTITYCOMP = rdoDEntity.SelectedIndex
            updateKYC.ISHOLDINGCOMP = rdoDHold.SelectedIndex

            updateKYC.APPROVECODE = ddlDApprove.SelectedValue
            updateKYC.APPROVENAME = txtDApproveNo.Text.Trim
            updateKYC.APPROVENO = txtDApproveNo.Text.Trim
            If txtDApproveDate.Text = "" Then
                updateKYC.APPROVEDATE = Nothing
            Else
                Dim clsFormatDate As clsDateFormat = New clsDateFormat
                updateKYC.APPROVEDATE = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDApproveDate.Text)
                clsFormatDate = Nothing
            End If
            'If ddlDApprove.SelectedValue = 1 Then
            '    updateKYC.KYCCOMPLETE = "N"
            'Else
            If intAns1 = 0 Then
                updateKYC.KYCCOMPLETE = "N"
            Else
                updateKYC.KYCCOMPLETE = "Y" 'ถือว่าทำ KYC สมบูรณ์แล้ว
            End If


            'End If
        Else
            'If strCdd = "1" Then
            If intAns1 = 0 Then
                updateKYC.KYCCOMPLETE = "N"
            Else
                updateKYC.KYCCOMPLETE = "Y" 'ถือว่าทำ KYC สมบูรณ์แล้ว
            End If
            'Else
            '    updateKYC.KYCCOMPLETE = ""
            'End If

        End If
        'updateKYC.KYCCOMPLETE = "Y"
        updateKYC.UPDATEBY = clsSessionMenuRight.getUserName
        updateKYC.UPDATEDATE = Now.ToShortDateString

        If Request.QueryString("KYC") = 1 Then
            updateKYC.DEPUPDATE = "D"
        ElseIf Request.QueryString("KYC") = 2 Then
            updateKYC.DEPUPDATE = "L"
        ElseIf Request.QueryString("KYC") = 3 Then
            updateKYC.DEPUPDATE = "P"
        End If
        updateKYC.BRANCHCODE = clsSessionMenuRight.getBranchCode
        updateKYC.ZONECODE = clsSessionMenuRight.getZoneCode
        updateKYC.DEPTCODE = clsSessionMenuRight.getDeptCode
        updateKYC.ALLCODERIGHT = clsSessionMenuRight.getAllCodeRight
    End Sub

    Private Sub saveNewKYCLoan()

        If ds.Tables.Contains("GENKYC") Then
            ds.Tables.Remove("GENKYC")
        End If

        Dim sql As String = "select " & System.Configuration.ConfigurationSettings.AppSettings("SequentRKYC_S") & " from dual"
        ds.Tables.Add(clsDatabase.QueryDataSet(sql, "GENKYC").Tables("GENKYC").Copy)
        Dim key As Integer = ds.Tables("GENKYC").Rows(0)("NEXTVAL")

        Dim newKYC As New RKYC
        newKYC.KYCSEQ = key
        newKYC.CUSTSEQ = CustSeq
        newKYC.KYCDATE = Now
        newKYC.ANSWERTYPE = 4
        newKYC.TRANSTYPE = 2
        newKYC.ACCOUNTNO = TxtAccNo.Text.Trim
        newKYC.ACCOUNTNAME = txtAccName.Text.Trim
        newKYC.WORKERSTATUS = "0"
        Dim intAns1 As Integer = 0

        '''''Prachak ''''''''''''''''''''''''''''
        newKYC.ACCT_STATE = Me.lblACCT_STATE.Text

        If chkLAcc1.Checked = True Then
            newKYC.ANSWER101 = 0
            intAns1 += 1
        Else
            newKYC.ANSWER101 = 1
        End If
        If chkLAcc2.Checked = True Then
            newKYC.ANSWER102 = 0
            intAns1 += 1
        Else
            newKYC.ANSWER102 = 1
        End If
        If chkLAcc3.Checked = True Then
            newKYC.ANSWER103 = 0
            intAns1 += 1
        Else
            newKYC.ANSWER103 = 1
        End If
        If chkLAcc4.Checked = True Then
            newKYC.ANSWER104 = 0
            intAns1 += 1
        Else
            newKYC.ANSWER104 = 1
        End If
        If chkLAcc5.Checked = True Then
            newKYC.ANSWER105 = 0
            intAns1 += 1
        Else
            newKYC.ANSWER105 = 1
        End If
        If chkLAcc6.Checked = True Then
            newKYC.ANSWER106 = 0
            intAns1 += 1
        Else
            newKYC.ANSWER106 = 1
        End If
        If chkLAcc7.Checked = True Then
            newKYC.ANSWER107 = 0
            intAns1 += 1
            newKYC.ANSWER199 = txtLAccOther.Text.Trim
        Else
            newKYC.ANSWER107 = 1
        End If

        newKYC.REF101 = ddlLBoard1.SelectedValue
        newKYC.REF102 = ddlLBoard2.SelectedValue
        newKYC.REF103 = ddlLRef1.SelectedValue
        newKYC.REF104 = txtLRef1.Text.Trim
        newKYC.REF201 = ddlLBoard3.SelectedValue
        newKYC.REF202 = ddlLBoard4.SelectedValue
        newKYC.REF203 = ddlLRef2.SelectedValue
        newKYC.REF204 = txtLRef2.Text.Trim
        newKYC.REF301 = ddlLBoard5.SelectedValue
        newKYC.REF302 = ddlLBoard6.SelectedValue
        newKYC.REF303 = ddlLRef3.SelectedValue
        newKYC.REF304 = txtLRef3.Text.Trim
        newKYC.REF401 = ddlLBoard7.SelectedValue
        newKYC.REF402 = ddlLBoard8.SelectedValue
        newKYC.REF403 = ddlLRef4.SelectedValue
        newKYC.REF404 = txtLRef4.Text.Trim
        newKYC.REF501 = ddlLBoard9.SelectedValue
        newKYC.REF502 = ddlLBoard10.SelectedValue
        newKYC.REF503 = ddlLRef5.SelectedValue
        newKYC.REF504 = txtLRef5.Text.Trim


        If PanelDepositSactionC.Visible = True Then
            If chkDAsset1.Checked = True Then
                newKYC.ASSET1 = 0
            Else
                newKYC.ASSET1 = 1
            End If
            If chkDAsset2.Checked = True Then
                newKYC.ASSET2 = 0
            Else
                newKYC.ASSET2 = 1
            End If
            If chkDAsset3.Checked = True Then
                newKYC.ASSET3 = 0
            Else
                newKYC.ASSET3 = 1
            End If
            If chkDAsset4.Checked = True Then
                newKYC.ASSET4 = 0
            Else
                newKYC.ASSET4 = 1
            End If
            If chkDAsset5.Checked = True Then
                newKYC.ASSET5 = 0
                newKYC.ASSETOTHER = txtDAssetOther.Text
            Else
                newKYC.ASSET5 = 1
            End If

            If txtDValue.Text = "" Then
                newKYC.TOTALASSET = Nothing
            Else
                clsMoney = New clsMoney
                newKYC.TOTALASSET = clsMoney.deleteComma(txtDValue.Text.Trim)
                clsMoney = Nothing
            End If

            newKYC.APPROVECODE = ddlDApprove.SelectedValue
            newKYC.APPROVENAME = txtDApproveNo.Text.Trim
            newKYC.APPROVENO = txtDApproveNo.Text.Trim
            If txtDApproveDate.Text = "" Then
                newKYC.APPROVEDATE = Nothing
            Else
                Dim clsFormatDate As clsDateFormat = New clsDateFormat
                newKYC.APPROVEDATE = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDApproveDate.Text)
                clsFormatDate = Nothing
            End If
            'If ddlDApprove.SelectedValue = 1 Then
            '    newKYC.KYCCOMPLETE = "N"
            'Else
            If intAns1 = 0 Then
                newKYC.KYCCOMPLETE = "N"
            Else
                newKYC.KYCCOMPLETE = "Y" 'ถือว่าทำ KYC สมบูรณ์แล้ว
            End If

            'End If
        Else
            'If strCdd = "1" Then
            If intAns1 = 0 Then
                newKYC.KYCCOMPLETE = "N"
            Else
                newKYC.KYCCOMPLETE = "Y" 'ถือว่าทำ KYC สมบูรณ์แล้ว
            End If
            'Else
            '    newKYC.KYCCOMPLETE = ""
            'End If

        End If

        newKYC.UPDATEBY = clsSessionMenuRight.getUserName
        newKYC.UPDATEDATE = Now.ToShortDateString

        If Request.QueryString("KYC") = 1 Then
            newKYC.DEPUPDATE = "D"
        ElseIf Request.QueryString("KYC") = 2 Then
            newKYC.DEPUPDATE = "L"
        ElseIf Request.QueryString("KYC") = 3 Then
            newKYC.DEPUPDATE = "P"
        End If
        newKYC.BRANCHCODE = clsSessionMenuRight.getBranchCode
        newKYC.ZONECODE = clsSessionMenuRight.getZoneCode
        newKYC.DEPTCODE = clsSessionMenuRight.getDeptCode
        newKYC.ALLCODERIGHT = clsSessionMenuRight.getAllCodeRight

        KYCEntity.AddToRKYC(newKYC)
    End Sub

    Private Sub saveUpdateKYCPN()
        Dim intKycSeq As Integer = hdfKYCSeq.Value
        Dim updateKYC = (From k In KYCEntity.RKYC
                       Where k.CUSTSEQ = CustSeq And k.KYCSEQ = intKycSeq And k.ANSWERTYPE = 6
                       Select k).First()

        'updateKYC.CHECKADDRESS = ddlCheckAddress.SelectedValue
        updateKYC.ACCOUNTNO = TxtAccNo.Text.Trim
        updateKYC.ACCOUNTNAME = txtAccName.Text.Trim
        updateKYC.WORKERSTATUS = "0"

        ''''Prachak'''''''''''''''''''''''''''''''''
        updateKYC.ACCT_STATE = Me.lblACCT_STATE.Text

        Dim intAns1 As Integer = 0
        Dim intAns2 As Integer = 0

        If chkPAcc1.Checked = True Then
            updateKYC.ANSWER101 = 0
            intAns1 += 1
        Else
            updateKYC.ANSWER101 = 1
        End If
        If chkPAcc2.Checked = True Then
            updateKYC.ANSWER102 = 0
            intAns1 += 1
        Else
            updateKYC.ANSWER102 = 1
        End If
        If chkPAcc3.Checked = True Then
            updateKYC.ANSWER103 = 0
            intAns1 += 1
        Else
            updateKYC.ANSWER103 = 1
        End If
        If chkPAcc4.Checked = True Then
            updateKYC.ANSWER104 = 0
            intAns1 += 1
            updateKYC.ANSWER199 = txtPAccOther.Text.Trim
        Else
            updateKYC.ANSWER104 = 1
        End If

        If chkPSource1.Checked = True Then
            updateKYC.ANSWER201 = 0

            intAns2 += 1
        Else
            updateKYC.ANSWER201 = 1
        End If
        If chkPSource2.Checked = True Then
            updateKYC.ANSWER202 = 0
            intAns2 += 1
        Else
            updateKYC.ANSWER202 = 1
        End If
        If chkPSource3.Checked = True Then
            updateKYC.ANSWER203 = 0
            intAns2 += 1
        Else
            updateKYC.ANSWER203 = 1
        End If
        If chkPSource4.Checked = True Then
            updateKYC.ANSWER204 = 0
            intAns2 += 1
        Else
            updateKYC.ANSWER204 = 1
        End If
        If chkPSource5.Checked = True Then
            updateKYC.ANSWER205 = 0
            intAns2 += 1
            updateKYC.ANSWER299 = txtPSourceOther.Text.Trim
        Else
            updateKYC.ANSWER205 = 1
        End If


        If rdoPRef.Items(0).Selected = True Then 'มีความสัมพันธ์
            updateKYC.ANSWER301 = 0
        Else
            updateKYC.ANSWER301 = 1
        End If

        updateKYC.REF101 = ddlPBoard1.SelectedValue
        updateKYC.REF102 = ddlPBoard2.SelectedValue
        updateKYC.REF103 = ddlPRef1.SelectedValue
        updateKYC.REF104 = txtPRef1.Text.Trim
        updateKYC.REF201 = ddlPBoard3.SelectedValue
        updateKYC.REF202 = ddlPBoard4.SelectedValue
        updateKYC.REF203 = ddlPRef2.SelectedValue
        updateKYC.REF204 = txtPRef2.Text.Trim


        updateKYC.ANSWER401 = ddlPDeposit.SelectedValue
        updateKYC.ANSWER501 = ddlPMoney.SelectedValue


        If PanelDepositSactionC.Visible = True Then
            If chkDAsset1.Checked = True Then
                updateKYC.ASSET1 = 0
            Else
                updateKYC.ASSET1 = 1
            End If
            If chkDAsset2.Checked = True Then
                updateKYC.ASSET2 = 0
            Else
                updateKYC.ASSET2 = 1
            End If
            If chkDAsset3.Checked = True Then
                updateKYC.ASSET3 = 0
            Else
                updateKYC.ASSET3 = 1
            End If
            If chkDAsset4.Checked = True Then
                updateKYC.ASSET4 = 0
            Else
                updateKYC.ASSET4 = 1
            End If
            If chkDAsset5.Checked = True Then
                updateKYC.ASSET5 = 0
                updateKYC.ASSETOTHER = txtDAssetOther.Text
            Else
                updateKYC.ASSET5 = 1
            End If

            If txtDValue.Text = "" Then
                updateKYC.TOTALASSET = Nothing
            Else
                clsMoney = New clsMoney
                updateKYC.TOTALASSET = clsMoney.deleteComma(txtDValue.Text.Trim)
                clsMoney = Nothing
            End If

            updateKYC.ISENTITYCOMP = rdoDEntity.SelectedIndex
            updateKYC.ISHOLDINGCOMP = rdoDHold.SelectedIndex

            updateKYC.APPROVECODE = ddlDApprove.SelectedValue
            updateKYC.APPROVENAME = txtDApproveNo.Text.Trim
            updateKYC.APPROVENO = txtDApproveNo.Text.Trim
            If txtDApproveDate.Text = "" Then
                updateKYC.APPROVEDATE = Nothing
            Else
                Dim clsFormatDate As clsDateFormat = New clsDateFormat
                updateKYC.APPROVEDATE = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDApproveDate.Text)
                clsFormatDate = Nothing
            End If

            'If ddlDApprove.SelectedValue = 1 Then
            '    updateKYC.KYCCOMPLETE = "N"
            'Else
            If intAns1 = 0 Or intAns2 = 0 Or ddlPDeposit.SelectedIndex = 0 Or ddlPMoney.SelectedIndex = 0 Then
                updateKYC.KYCCOMPLETE = "N"
            Else
                updateKYC.KYCCOMPLETE = "Y" 'ถือว่าทำ KYC สมบูรณ์แล้ว
            End If



            'End If
        Else
            'If strCdd = "1" Then
            If intAns1 = 0 Or intAns2 = 0 Or ddlPDeposit.SelectedIndex = 0 Or ddlPMoney.SelectedIndex = 0 Then
                updateKYC.KYCCOMPLETE = "N"
            Else
                updateKYC.KYCCOMPLETE = "Y" 'ถือว่าทำ KYC สมบูรณ์แล้ว
            End If
            'Else
            '    updateKYC.KYCCOMPLETE = ""
            'End If

        End If
        updateKYC.UPDATEBY = clsSessionMenuRight.getUserName
        updateKYC.UPDATEDATE = Now.ToShortDateString

        If Request.QueryString("KYC") = 1 Then
            updateKYC.DEPUPDATE = "D"
        ElseIf Request.QueryString("KYC") = 2 Then
            updateKYC.DEPUPDATE = "L"
        ElseIf Request.QueryString("KYC") = 3 Then
            updateKYC.DEPUPDATE = "P"
        End If
        updateKYC.BRANCHCODE = clsSessionMenuRight.getBranchCode
        updateKYC.ZONECODE = clsSessionMenuRight.getZoneCode
        updateKYC.DEPTCODE = clsSessionMenuRight.getDeptCode
        updateKYC.ALLCODERIGHT = clsSessionMenuRight.getAllCodeRight
    End Sub

    Private Sub saveNewKYCPN()

        If ds.Tables.Contains("GENKYC") Then
            ds.Tables.Remove("GENKYC")
        End If

        Dim sql As String = "select " & System.Configuration.ConfigurationSettings.AppSettings("SequentRKYC_S") & " from dual"
        ds.Tables.Add(clsDatabase.QueryDataSet(sql, "GENKYC").Tables("GENKYC").Copy)
        Dim key As Integer = ds.Tables("GENKYC").Rows(0)("NEXTVAL")

        Dim newKYC As New RKYC
        newKYC.KYCSEQ = key
        newKYC.CUSTSEQ = CustSeq
        newKYC.KYCDATE = Now
        newKYC.ANSWERTYPE = 6
        newKYC.TRANSTYPE = 2
        newKYC.ACCOUNTNO = TxtAccNo.Text.Trim
        newKYC.ACCOUNTNAME = txtAccName.Text.Trim
        newKYC.WORKERSTATUS = "0"

        '''''Prachak ''''''''''''''''''''''''''''
        newKYC.ACCT_STATE = Me.lblACCT_STATE.Text

        Dim intAns1 As Integer = 0
        Dim intAns2 As Integer = 0

        If chkPAcc1.Checked = True Then
            newKYC.ANSWER101 = 0
            intAns1 += 1
        Else
            newKYC.ANSWER101 = 1
        End If
        If chkPAcc2.Checked = True Then
            newKYC.ANSWER102 = 0
            intAns1 += 1
        Else
            newKYC.ANSWER102 = 1
        End If
        If chkPAcc3.Checked = True Then
            newKYC.ANSWER103 = 0
            intAns1 += 1
        Else
            newKYC.ANSWER103 = 1
        End If
        If chkPAcc4.Checked = True Then
            newKYC.ANSWER104 = 0
            intAns1 += 1
            newKYC.ANSWER199 = txtPAccOther.Text.Trim
        Else
            newKYC.ANSWER104 = 1
        End If

        If chkPSource1.Checked = True Then
            newKYC.ANSWER201 = 0
            intAns2 += 1
        Else
            newKYC.ANSWER201 = 1
        End If
        If chkPSource2.Checked = True Then
            newKYC.ANSWER202 = 0
            intAns2 += 1
        Else
            newKYC.ANSWER202 = 1
        End If
        If chkPSource3.Checked = True Then
            newKYC.ANSWER203 = 0
            intAns2 += 1
        Else
            newKYC.ANSWER203 = 1
        End If
        If chkPSource4.Checked = True Then
            newKYC.ANSWER204 = 0
            intAns2 += 1
        Else
            newKYC.ANSWER204 = 1
        End If
        If chkPSource5.Checked = True Then
            newKYC.ANSWER205 = 0
            intAns2 += 1
            newKYC.ANSWER299 = txtPSourceOther.Text.Trim
        Else
            newKYC.ANSWER205 = 1
        End If


        If rdoPRef.Items(0).Selected = True Then 'มีความสัมพันธ์
            newKYC.ANSWER301 = 0
        Else
            newKYC.ANSWER301 = 1
        End If

        newKYC.REF101 = ddlPBoard1.SelectedValue
        newKYC.REF102 = ddlPBoard2.SelectedValue
        newKYC.REF103 = ddlPRef1.SelectedValue
        newKYC.REF104 = txtPRef1.Text.Trim
        newKYC.REF201 = ddlPBoard3.SelectedValue
        newKYC.REF202 = ddlPBoard4.SelectedValue
        newKYC.REF203 = ddlPRef2.SelectedValue
        newKYC.REF204 = txtPRef2.Text.Trim


        newKYC.ANSWER401 = ddlPDeposit.SelectedValue
        newKYC.ANSWER501 = ddlPMoney.SelectedValue

        If PanelDepositSactionC.Visible = True Then
            If chkDAsset1.Checked = True Then
                newKYC.ASSET1 = 0
            Else
                newKYC.ASSET1 = 1
            End If
            If chkDAsset2.Checked = True Then
                newKYC.ASSET2 = 0
            Else
                newKYC.ASSET2 = 1
            End If
            If chkDAsset3.Checked = True Then
                newKYC.ASSET3 = 0
            Else
                newKYC.ASSET3 = 1
            End If
            If chkDAsset4.Checked = True Then
                newKYC.ASSET4 = 0
            Else
                newKYC.ASSET4 = 1
            End If
            If chkDAsset5.Checked = True Then
                newKYC.ASSET5 = 0
                newKYC.ASSETOTHER = txtDAssetOther.Text
            Else
                newKYC.ASSET5 = 1
            End If

            If txtDValue.Text = "" Then
                newKYC.TOTALASSET = Nothing
            Else
                clsMoney = New clsMoney
                newKYC.TOTALASSET = clsMoney.deleteComma(txtDValue.Text.Trim)
                clsMoney = Nothing
            End If

            newKYC.APPROVECODE = ddlDApprove.SelectedValue
            newKYC.APPROVENAME = txtDApproveNo.Text.Trim
            newKYC.APPROVENO = txtDApproveNo.Text.Trim
            If txtDApproveDate.Text = "" Then
                newKYC.APPROVEDATE = Nothing
            Else
                Dim clsFormatDate As clsDateFormat = New clsDateFormat
                newKYC.APPROVEDATE = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDApproveDate.Text)
                clsFormatDate = Nothing
            End If
            'If ddlDApprove.SelectedValue = 1 Then
            '    newKYC.KYCCOMPLETE = "N"
            'Else
            If intAns1 = 0 Or intAns2 = 0 Or ddlPDeposit.SelectedIndex = 0 Or ddlPMoney.SelectedIndex = 0 Then
                newKYC.KYCCOMPLETE = "N"
            Else
                newKYC.KYCCOMPLETE = "Y" 'ถือว่าทำ KYC สมบูรณ์แล้ว
            End If


            'End If
        Else
            'If strCdd = "1" Then
            If intAns1 = 0 Or intAns2 = 0 Or ddlPDeposit.SelectedIndex = 0 Or ddlPMoney.SelectedIndex = 0 Then
                newKYC.KYCCOMPLETE = "N"
            Else
                newKYC.KYCCOMPLETE = "Y" 'ถือว่าทำ KYC สมบูรณ์แล้ว
            End If
            'Else
            '    newKYC.KYCCOMPLETE = ""
            'End If

        End If
        newKYC.UPDATEBY = clsSessionMenuRight.getUserName
        newKYC.UPDATEDATE = Now.ToShortDateString

        If Request.QueryString("KYC") = 1 Then
            newKYC.DEPUPDATE = "D"
        ElseIf Request.QueryString("KYC") = 2 Then
            newKYC.DEPUPDATE = "L"
        ElseIf Request.QueryString("KYC") = 3 Then
            newKYC.DEPUPDATE = "P"
        End If
        newKYC.BRANCHCODE = clsSessionMenuRight.getBranchCode
        newKYC.ZONECODE = clsSessionMenuRight.getZoneCode
        newKYC.DEPTCODE = clsSessionMenuRight.getDeptCode
        newKYC.ALLCODERIGHT = clsSessionMenuRight.getAllCodeRight

        KYCEntity.AddToRKYC(newKYC)

    End Sub

    Private Sub SaveLog(ByVal index As Integer)
        Try
            Dim program As String = ""
            If CInt(Request.QueryString("KYC")) = 1 Then
                program = "M12"
            ElseIf CInt(Request.QueryString("KYC")) = 2 Then
                program = "M22"
            ElseIf CInt(Request.QueryString("KYC")) = 3 Then
                program = "M32"
            ElseIf CInt(Request.QueryString("KYC")) = 5 Then
                program = "M52"
            Else
                program = "M42"
            End If

            Dim desc As String = ""
            If index = 1 Then
                desc = "พิมพ์ฟอร์ม KYC"
            ElseIf index = 2 Then
                desc = "พิมพ์ผลประเมินความเสี่ยง"
            ElseIf index = 3 Then
                desc = "บันทึก ข้อมูล KYC (เลขทะเบียนนิติบุคคล " + txtBizNo.Text + ")"
            ElseIf index = 4 Then
                desc = "บันทึก แก้ไข ข้อมูล KYC (เลขทะเบียนนิติบุคคล " + txtBizNo.Text + ")"
            End If

            Dim clsLogTransaction As New clsLogTransaction
            If index < 3 Then
                clsLogTransaction.saveLogTransaction(txtBizNo.Text.Trim, clsSessionMenuRight.getUserName, 2, desc, program)
            Else
                clsLogTransaction.saveLogTransaction(txtBizNo.Text.Trim, clsSessionMenuRight.getUserName, index, desc, program)
            End If

            clsLogTransaction = Nothing
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

#End Region

#Region "Even GridView"



    Protected Sub grdRef_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdRef.SelectedIndexChanged

        'If (grdRef.SelectedIndex = intBeforeItemIndex) Then
        '    grdRef.SelectedIndex = -1
        'End If

        'If Not grdRef.SelectedIndex = -1 Then
        '    If grdRef.Rows(grdRef.SelectedIndex).Cells(7).Text = 1 Then
        '        btnShow.Attributes.Add("onclick", "window.showModalDialog('NonIndividualBoard.aspx?index=" & grdRef.SelectedIndex & "&TypeKYC=4', null, 'status:no; dialogWidth:1000px; dialogHeight:600px; help:no; scroll:yes; menubar:no; resizable:no')")
        '    Else
        '        btnShow.Attributes.Add("onclick", "window.showModalDialog('NonIndividualBoardNon.aspx?index=" & grdRef.SelectedIndex & "&TypeKYC=4', null, 'status:no; dialogWidth:1000px; dialogHeight:600px; help:no; scroll:yes; menubar:no; resizable:no')")
        '    End If
        'End If

    End Sub

    Protected Sub grdRef_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles grdRef.SelectedIndexChanging
        'intBeforeItemIndex = grdRef.SelectedIndex
    End Sub

#End Region

    Protected Sub btnCDD_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCDD.Click
        Try
            If CustSeq = 0 Then
                Response.Redirect("NonIndividualCDD.aspx?KYC=" & Request.QueryString("KYC") & "&TypeKYC=1&CUSTSEQ=" & CustSeq & "&Back=1")
            Else
                Response.Redirect("NonIndividualCDD.aspx?KYC=" & Request.QueryString("KYC") & "&TypeKYC=2&CUSTSEQ=" & CustSeq & "")
            End If
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub ddlFormalProvice_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlFormalProvice.SelectedIndexChanged
        Dim clsDropDown As New clsMasterDropDown

        If Not ddlFormalProvice.SelectedIndex = 0 Then
            clsDropDown.setAmphor(ddlFormalAmphor, ProviceCode:=ddlFormalProvice.SelectedValue)
        Else
            ddlFormalAmphor.Items.Clear()
            ddlFormalTambol.Items.Clear()
        End If
    End Sub

    Protected Sub ddlFormalAmphor_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlFormalAmphor.SelectedIndexChanged
        Dim clsDropDown As New clsMasterDropDown

        If Not ddlFormalAmphor.SelectedIndex = 0 Then
            clsDropDown.setTambol(ddlFormalTambol, AmphorCode:=ddlFormalAmphor.SelectedValue)
        Else
            ddlFormalTambol.Items.Clear()
        End If
    End Sub

    Protected Function concatAddress(ByVal strTextAddrNo As String, ByVal strTextMoo As String, _
                               ByVal strTextSoi As String, ByVal strTextStreet As String, _
                               ByVal strTextTambol As String, ByVal strTextAmphor As String, _
                               ByVal strTextProvince As String, ByVal strTextZip As String) As String

        Dim strFullAddress As String = ""


        Dim strAddressNo As String = ""
        Dim strMoo As String = " หมู่ "
        Dim strSoi As String = " ซอย "
        Dim strStreet As String = " ถนน "
        Dim strTamBol As String = " แขวง/ตำบล "
        Dim strAmphor As String = " เขต/อำเภอ "
        Dim strProvince As String = " จังหวัด "
        Dim strZip As String = " รหัสไปรษณีย์ "

        strAddressNo = If(strTextAddrNo.Trim = "", "-", strTextAddrNo.Trim)
        strMoo &= If(strTextMoo.Trim = "", "-", strTextMoo.Trim)
        strSoi &= If(strTextSoi.Trim = "", "-", strTextSoi.Trim)
        strStreet &= If(strTextStreet.Trim = "", "-", strTextStreet.Trim)
        strTamBol &= strTextTambol.Trim
        strAmphor &= strTextAmphor.Trim
        strProvince &= strTextProvince.Trim
        strZip &= If(strTextZip.Trim = "", "-", strTextZip.Trim)

        strFullAddress = strAddressNo & strMoo & strSoi & strStreet & strTamBol & strAmphor & strProvince & strZip

        Return strFullAddress
    End Function

    Protected Sub grdShowAccount_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdShowAccount.RowCommand
        If (e.CommandName = "Acc_Import") Then

            If Not String.IsNullOrEmpty(lbCUSTSEQ.Text) Then
                Try
                    If (grdShowAccount.SelectedIndex >= -1) Then
                        Dim accountNO As String = grdShowAccount.Rows(e.CommandArgument).Cells(1).Text
                        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", "window.open('../Administrator/KycFileScan.aspx?CUSTSEQ=" & lbCUSTSEQ.Text & "&accountNO=" & accountNO & "','_blank','scrollbars=1,Width:800px, Height:600px')", True)
                    End If
                Catch ex As Exception
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" & ex.Message & "')", True)
                    condition = False
                End Try
            End If
        End If

        If (e.CommandName = "Acc_View") Then


        End If
    End Sub

    Protected Sub grdShowAccount_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdShowAccount.RowDataBound
        If (e.Row.RowType.ToString = "DataRow") Then
            Dim lblAppe As Label = CType(e.Row.FindControl("label38"), Label)
            If (lblAppe.Text = "") Then
                lblAppe.Text = " - "
            ElseIf (lblAppe.Text = 1) Then
                lblAppe.Text = " รออนุมัติ "
            ElseIf (lblAppe.Text = 2) Then
                lblAppe.Text = " อนุมัติ "
            ElseIf (lblAppe.Text = 3) Then
                lblAppe.Text = " ไม่อนุม้ติ "
            End If
        End If
    End Sub

    Protected Sub grdShowAccount_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles grdShowAccount.SelectedIndexChanging

        intBeforeItemIndex = grdShowAccount.SelectedIndex
        'btnAdd.Visible = False
        'btnAddOld.Visible = True

        VisbleKYC()
    End Sub


    Private Sub resetData()
        'gvUserRight.DataSource = dsUserRight.Tables("sUserRight")
        'gvUserRight.DataBind()
        grdShowAccount.SelectedIndex = -1

        grdShowAccount.SelectedIndex = -1
        'TxtAccNo.Text = ""
        'txtAccName.Text = ""
        'hdfKYCSeq.Value = Nothing
        'TxtAccNo.Enabled = True
        'txtAccName.Enabled = True

        Me.TxtAccNo.Text = ""
        Me.txtAccName.Text = ""
        Me.lblACCT_STATE.Text = "S"
        hdfKYCSeq.Value = Nothing
        Me.TxtAccNo.Enabled = True
        Me.txtAccName.Enabled = True
        Me.btnSave.Visible = False
        Me.btAddAcc.Enabled = True
        Me.btDelAcc.Enabled = False
        Me.btnPOP_CBS.Enabled = True
        ''''end offer Script''''
        btAddBo.Text = "เพิ่มข้อมูล BO"
        lbOfAcc1.Text = ""
        lbOfAcc2.Text = ""
        lbOfAcc3.Text = ""
        ''''''''''''''''''''''''''''


        If PanelDeposit.Visible = True Then
            clearDataInControl(PanelDeposit)
        ElseIf PanelLoan.Visible = True Then
            clearDataInControl(PanelLoan)
        ElseIf PanelPN.Visible = True Then
            clearDataInControl(PanelPN)
        End If

    End Sub

    Private Sub clearDataInControl(ByVal plControl As Panel)
        Dim pl As Panel = plControl

        For Each i As Control In pl.Controls

            If TypeOf (i) Is CheckBox Then
                Dim chk As CheckBox = CType(i, CheckBox)
                chk.Checked = False
            End If

            If TypeOf (i) Is TextBox Then
                Dim txt As TextBox = CType(i, TextBox)
                txt.Text = ""
            End If

            If TypeOf (i) Is DropDownList Then
                Dim ddl As DropDownList = CType(i, DropDownList)
                ddl.SelectedIndex = 0
            End If

        Next

    End Sub

    Private Sub clearPanel()
        If PanelDeposit.Visible = True Then
            clearDataInControl(PanelDeposit)
        ElseIf PanelLoan.Visible = True Then
            clearDataInControl(PanelLoan)
        ElseIf PanelPN.Visible = True Then
            clearDataInControl(PanelPN)
        End If

    End Sub

    Dim intCountSearch As Integer = 0

    Protected Sub btnSearchAcc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearchAcc.Click
        Dim intKYC As Integer = CInt(Request.QueryString("KYC"))
        If intKYC = 1 Then
            intKYC = 2
        ElseIf intKYC = 2 Then
            intKYC = 4
        ElseIf intKYC = 3 Then
            intKYC = 6
        End If

        Dim strAcc As String = TxtAccNo.Text
        'Dim qryKYC = (From kyc In KYCEntity.RKYC
        '                  Where kyc.ACCOUNTNO = strAcc And kyc.ANSWERTYPE = intKYC
        '                  Select kyc.ACCOUNTNO, kyc.ACCOUNTNAME).ToList

        If hdfKYCSeq.Value = "" Then
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                      UpdatePanel1.GetType(), _
                                                      "clientScript", _
                                                      "alert('" + "กรุณาเลือกรายการที่จะทำแบบสอบถามเพิ่มเติม" + "')", _
                                                      True)

            Exit Sub
        End If
        Dim intkycSeq As Integer = hdfKYCSeq.Value
        If Request.QueryString("KYC") = 1 Then
            BindingDataKYCDeposit(strAcc)
        ElseIf Request.QueryString("KYC") = 2 Then
            BindingDataKYCLoan(strAcc)
        ElseIf Request.QueryString("KYC") = 3 Then
            BindingDataKYCPN(strAcc)
        End If

        If intCountSearch = 0 Then
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                          UpdatePanel1.GetType(), _
                                                          "clientScript", _
                                                          "alert('" + "ไม่พบข้อมูล" + "')", _
                                                          True)
            TxtAccNo.Enabled = True
            txtAccName.Enabled = True
        Else

            TxtAccNo.Enabled = False
            txtAccName.Enabled = False

        End If

        'If Not qryKYC.Count = 0 Then
        '    TxtAccNo.Text = qryKYC(0).ACCOUNTNO
        '    txtAccName.Text = qryKYC(0).ACCOUNTNAME
        'Else
        '    ScriptManager.RegisterStartupScript(UpdatePanel1, _
        '                                                  UpdatePanel1.GetType(), _
        '                                                  "clientScript", _
        '                                                  "alert('" + "ไม่พบข้อมูล" + "')", _
        '                                                  True)
        'End If

    End Sub


    Protected Sub ddlDApprove_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDApprove.SelectedIndexChanged
        If ddlDApprove.SelectedIndex = 0 Then
            txtDApproveNo.Enabled = False
            txtApprovePassword.Enabled = False
            txtDApproveNo.Text = ""
            txtApprovePassword.Text = ""
        Else
            If ddlDApprove.SelectedValue = 1 Then
                txtDApproveNo.Enabled = False
                txtApprovePassword.Enabled = False
                txtDApproveNo.Text = ""
                txtApprovePassword.Text = ""
            Else
                txtDApproveNo.Enabled = True
                txtApprovePassword.Enabled = True
            End If
        End If
    End Sub

    Protected Sub btnSaveAcc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDeleteAcc.Click


        If grdShowAccount.SelectedIndex > -1 Then
            Dim strAccountNo As String = ""
            strAccountNo = grdShowAccount.Rows(grdShowAccount.SelectedIndex).Cells(1).Text.Trim()

            If Not strAccountNo.Trim = "" Then
                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" & "ไม่สามารถลบได้" & "')", True)
                Exit Sub
            End If

            Dim strBranch As String = grdShowAccount.Rows(grdShowAccount.SelectedIndex).Cells(7).Text.Trim
            Dim strUpdateby As String = grdShowAccount.Rows(grdShowAccount.SelectedIndex).Cells(6).Text.Trim
            If Not (strUpdateby.Substring(0, 3) = "ETL") Then
                If Not (clsSessionMenuRight.getBranchCode = strBranch) Then

                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" & "ไม่สามารถลบข้อมูลต่างสาขาได้" & "')", True)
                    Exit Sub
                End If
            End If


            KYCEntity = New KYCEntities
            KYCEntity.Connection.Open()
            Using transaction As System.Data.Common.DbTransaction = KYCEntity.Connection.BeginTransaction()
                Try
                    Dim strKYCSEQ As String = CType(grdShowAccount.Rows(grdShowAccount.SelectedIndex).FindControl("lblKYCSeq"), Label).Text

                    'delete account
                    Dim qry = (From R_KYC In KYCEntity.RKYC Where R_KYC.CUSTSEQ = CustSeq And R_KYC.KYCSEQ = strKYCSEQ).ToList
                    KYCEntity.DeleteObject(qry(0))
                    KYCEntity.SaveChanges()


                    Dim intCountComplete As Integer = 0
                    For i As Integer = 0 To ds.Tables("rkyc").Rows.Count - 1
                        If Not strKYCSEQ = ds.Tables("rkyc").Rows(i).Item("KYCSEQ").ToString Then
                            If ds.Tables("rkyc").Rows(i).Item("KYCCOMPLETE").ToString.Trim = "N" Then
                                intCountComplete += 1
                            End If
                        End If
                    Next
                    Dim updateCust = (From Cust In KYCEntity.MCUSTOMER
                             Where Cust.CUSTSEQ = CustSeq
                             Select Cust).First()
                    If Request.QueryString("KYC") = 1 Then
                        If intCountComplete = 0 Then
                            updateCust.DEPD = "Y"
                        Else
                            updateCust.DEPD = "N"
                        End If
                    ElseIf Request.QueryString("KYC") = 2 Then
                        If intCountComplete = 0 Then
                            updateCust.DEPL = "Y"
                        Else
                            updateCust.DEPL = "N"
                        End If
                    ElseIf Request.QueryString("KYC") = 3 Then
                        If intCountComplete = 0 Then
                            updateCust.DEPP = "Y"
                        Else
                            updateCust.DEPP = "N"
                        End If
                    End If
                    transaction.Commit()
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" & "ทำการลบเสร็จเรียบร้อยแล้ว" & "')", True)

                Catch ex As Exception
                    transaction.Rollback()
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('Error : " & ex.ToString & " Error message : " & ex.Message & "')", True)
                End Try

            End Using

            KYCEntity.SaveChanges()
            loadKYCAccount()
            grdShowAccount.SelectedIndex = -1
            KYCEntity = Nothing
        End If
    End Sub

    Private Function chkDataKYC(ByVal strKyc As String) As Boolean
        Dim blnChk As Boolean = True
        Dim intAns1 As Integer = 0
        Dim intAns2 As Integer = 0
        Dim intSource As Integer = 0

        If strKyc = 1 Then
            intAns1 = 0
            If chkDAcc1.Checked = True Then
                intAns1 += 1
            End If
            If chkDAcc2.Checked = True Then
                intAns1 += 1
            End If
            If chkDAcc3.Checked = True Then
                intAns1 += 1
            End If
            If chkDAcc4.Checked = True Then
                intAns1 += 1
            End If

            If chkDSource1.Checked = True Then
                intAns2 += 1
            End If
            If chkDSource2.Checked = True Then
                intAns2 += 1
            End If
            If chkDSource3.Checked = True Then
                intAns2 += 1
            End If
            If chkDSource4.Checked = True Then
                intAns2 += 1
            End If
            If chkDSource5.Checked = True Then
                intAns2 += 1
            End If
            If intAns1 = 0 Or intAns2 = 0 Or ddlDDeposit.SelectedIndex = 0 Or ddlDMoney.SelectedIndex = 0 Then
                blnChk = False
            Else
                blnChk = True
            End If

        ElseIf strKyc = 2 Then
            intAns1 = 0
            If chkLAcc1.Checked = True Then
                intAns1 += 1
            End If
            If chkLAcc2.Checked = True Then
                intAns1 += 1
            End If
            If chkLAcc3.Checked = True Then
                intAns1 += 1
            End If
            If chkLAcc4.Checked = True Then
                intAns1 += 1
            End If
            If chkLAcc5.Checked = True Then
                intAns1 += 1
            End If
            If chkLAcc6.Checked = True Then
                intAns1 += 1
            End If
            If chkLAcc7.Checked = True Then
                intAns1 += 1
            End If
            If intAns1 = 0 Then
                blnChk = False
            Else
                blnChk = True
            End If

        ElseIf strKyc = 3 Then
            intAns1 = 0
            intAns2 = 0

            If chkPAcc1.Checked = True Then
                intAns1 += 1
            End If
            If chkPAcc2.Checked = True Then
                intAns1 += 1
            End If
            If chkPAcc3.Checked = True Then
                intAns1 += 1
            End If
            If chkPAcc4.Checked = True Then
                intAns1 += 1
            End If

            If chkPSource1.Checked = True Then
                intAns2 += 1
            End If
            If chkPSource2.Checked = True Then
                intAns2 += 1
            End If
            If chkPSource3.Checked = True Then
                intAns2 += 1
            End If
            If chkPSource4.Checked = True Then
                intAns2 += 1
            End If
            If chkPSource5.Checked = True Then
                intAns2 += 1
            End If
            If intAns1 = 0 Or intAns2 = 0 Or ddlPDeposit.SelectedIndex = 0 Or ddlPMoney.SelectedIndex = 0 Then
                blnChk = False
            Else
                blnChk = True
            End If


        End If

        Return blnChk

    End Function

    Protected Sub btnKYC_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnKYC.Click
        Try
            If CInt(Session("KYC")) = 5 Then
                hdfKYCSeq.Value = "0"
            End If
            If hdfKYCSeq.Value = "" Then
                ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                          UpdatePanel1.GetType(), _
                                                          "clientScript", _
                                                          "alert('" + "กรุณาเลือกรายการที่จะพิมพ์" + "')", _
                                                          True)

                Exit Sub
            End If
            SaveLog(1)
            If CInt(Session("KYC")) = 1 Then
                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "window.open('../Form/KYC1T_02.aspx?CUSTSEQ=" & CInt(CustSeq) & "&Type=" & CInt(Session("KYC")) & "&KYCSEQ=" & hdfKYCSeq.Value & "','_blank','scrollbars=1,Width:1000px, Height:600px')", True)
            ElseIf CInt(Session("KYC")) = 2 Then
                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "window.open('../Form/KYC2T_02.aspx?CUSTSEQ=" & CInt(CustSeq) & "&KYCSEQ=" & hdfKYCSeq.Value & "','_blank','scrollbars=1,Width:1000px, Height:600px')", True)
            ElseIf CInt(Session("KYC")) = 3 Then
                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "window.open('../Form/KYC3T_02.aspx?CUSTSEQ=" & CInt(CustSeq) & "&Type=" & CInt(Session("KYC")) & "&KYCSEQ=" & hdfKYCSeq.Value & "','_blank','scrollbars=1,Width:1000px, Height:600px')", True)

            ElseIf CInt(Session("KYC")) = 5 Then
                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "window.open('../Form/KYC4T_02.aspx?CUSTSEQ=" & CInt(CustSeq) & "','_blank','scrollbars=1,Width:1000px, Height:600px')", True)


            End If
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)

        End Try
    End Sub

#Region "ประเภทธุรกิจ"

    Sub Bind_Data_togrid_Corp_Category()

        '''' Connect Data to Grid '''''''''''

        Me.dtC_Cust_Category.Columns.Add("CAT_DESCRIPTION")
        Me.dtC_Cust_Category.Columns.Add("CAT_CODE")
        Me.dtC_Cust_Category.Columns.Add("REMARKS")
        Me.dtC_Cust_Category.Columns.Add("REMARKS_OTH")
        Me.dtC_Cust_Category.Columns.Add("RISK")
        Me.dtC_Cust_Category.Columns.Add("ACTIVE")

        Dim lvsCustType As Integer = Session("KYC")

        Dim QryOthDoc = (From tbName In KYCEntity.MCUST_C_CATE_TYPE
                           Where tbName.CUSTSEQ = CustSeq
                          Select tbName.SEQ,
                                 tbName.CAT_DESCRIPTION,
                                 tbName.CAT_CODE,
                                 tbName.REMARKS,
                                 tbName.RISK,
                                 tbName.REMARKS_OTH,
                                 ACTIVE = If(tbName.ACTIVE = "Y", True, False) Order By SEQ)

        For Each mRow In QryOthDoc
            Me.dtC_Cust_Category.Rows.Add(mRow.CAT_DESCRIPTION,
                                  mRow.CAT_CODE,
                                  mRow.REMARKS,
                                  mRow.REMARKS_OTH,
                                  mRow.RISK,
                                  mRow.ACTIVE)

            Me.dtC_Cust_Category.AcceptChanges()
        Next

        Me.grdCorp_Cate.DataSource = Me.dtC_Cust_Category
        Me.grdCorp_Cate.DataBind()

        ''''จำนวนแถว ''''''''''''''''''''''''''''''''''''''
        Me.lblCount_Corp_Cate.Text = Me.dtC_Cust_Category.Rows.Count
        ''''''''''''''''''''''''''''''''''''''''''''''''''

        Session("PopCorpCate") = Me.dtC_Cust_Category


    End Sub

    Protected Sub chACORP_CATE_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        ''''check Session ''''''''''''''''''''''
        If Not (Session("PopCorpCate") Is Nothing) Then
            Me.dtC_Cust_Category = Session("PopCorpCate")
        End If

        '''''' assign checkbox value to data table 
        For i = 0 To Me.grdCorp_Cate.Rows.Count - 1
            Me.dtC_Cust_Category.Rows(i).Item("ACTIVE") = CType(Me.grdCorp_Cate.Rows(i).Cells(5).FindControl("chACORP_CATE"), CheckBox).Checked
        Next i

        Me.dtC_Cust_Category.AcceptChanges()
        Me.grdCorp_Cate.DataSource = Me.dtC_Cust_Category
        Me.grdCorp_Cate.DataBind()

        Session("PopCorpCate") = Me.dtC_Cust_Category

    End Sub

#End Region

#Region "นักการเมือง"

    Sub Bind_Data_togrid_grdPolitical()

        '''' Connect Data to Grid '''''''''''
        Me.dtPolitical.Columns.Add("THPREFIX")
        Me.dtPolitical.Columns.Add("THPREFIX_DESC")
        Me.dtPolitical.Columns.Add("THFIRSTNAME")
        Me.dtPolitical.Columns.Add("THMIDNAME")
        Me.dtPolitical.Columns.Add("THLASTNAME")

        Me.dtPolitical.Columns.Add("ENPREFIX")
        Me.dtPolitical.Columns.Add("ENPREFIX_DESC")
        Me.dtPolitical.Columns.Add("ENFIRSTNAME")
        Me.dtPolitical.Columns.Add("ENMIDNAME")
        Me.dtPolitical.Columns.Add("ENLASTNAME")

        Me.dtPolitical.Columns.Add("POLITIC_POST_CONC")
        Me.dtPolitical.Columns.Add("POLITIC_RELATION")
        Me.dtPolitical.Columns.Add("ACTIVE")

        Dim lvsCustType As Integer = Session("KYC")

        Dim qryPolitic = (From tbName In KYCEntity.MCUST_POLITICIAN_CONCERN
                        Where tbName.CUSTSEQ = CustSeq
                       Select tbName.SEQ,
                              tbName.THPREFIX,
                              tbName.THPREFIX_DESC,
                              tbName.THFIRSTNAME,
                              tbName.THMIDNAME,
                              tbName.THLASTNAME,
                              tbName.ENPREFIX,
                              tbName.ENPREFIX_DESC,
                              tbName.ENFIRSTNAME,
                              tbName.ENMIDNAME,
                              tbName.ENLASTNAME,
                              tbName.POLITIC_RELATION,
                              tbName.POLITIC_POST_CONC,
                              ACTIVE = If(tbName.ACTIVE = "Y", True, False)
                     Order By SEQ)

        For Each mRow In qryPolitic
            Me.dtPolitical.Rows.Add(mRow.THPREFIX,
                                    mRow.THPREFIX_DESC,
                                    mRow.THFIRSTNAME,
                                    mRow.THMIDNAME,
                                    mRow.THLASTNAME,
                                    mRow.ENPREFIX,
                                    mRow.ENPREFIX_DESC,
                                    mRow.ENFIRSTNAME,
                                    mRow.THMIDNAME,
                                    mRow.ENLASTNAME,
                                    mRow.POLITIC_POST_CONC,
                                    mRow.POLITIC_RELATION,
                                    mRow.ACTIVE)
            Me.dtPolitical.AcceptChanges()
        Next

        Me.grdPolitical.DataSource = Me.dtPolitical
        Me.grdPolitical.DataBind()
        ''''Number of Rows '''''''''''''''''''''''''''''''''' 
        Me.lblCount_Politic.Text = Me.dtPolitical.Rows.Count
        '''''''''''''''''''''''''''''''''''''''''''''''''''''
        Session("PopPolitical") = Me.dtPolitical

    End Sub

    Protected Sub chk_ACT_POLTIC_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        ''''check Session ''''''''''''''''''''''
        If Not (Session("PopPolitical") Is Nothing) Then
            Me.dtPolitical = Session("PopPolitical")
        End If

        '''''' assign checkbox value to data table 
        For i = 0 To Me.grdPolitical.Rows.Count - 1
            Me.dtPolitical.Rows(i).Item("ACTIVE") = CType(Me.grdPolitical.Rows(i).Cells(6).FindControl("chk_ACT_POLTIC"), CheckBox).Checked
        Next i

        Me.dtPolitical.AcceptChanges()
        Me.grdPolitical.DataSource = Me.dtPolitical
        Me.grdPolitical.DataBind()

        Session("PopPolitical") = Me.dtPolitical

    End Sub

#End Region

#Region "แหล่งที่มาของรายได้"

    Sub Bind_Data_togrid_Income_Source()

        '''' Connect Data to Grid '''''''''''
        Me.dtIncome.Columns.Add("COUNTYNAME")
        Me.dtIncome.Columns.Add("NATIONCODE")
        Me.dtIncome.Columns.Add("COUNTYNAMETH")
        Me.dtIncome.Columns.Add("RISK_LEVEL")
        Me.dtIncome.Columns.Add("ACTIVE")

        Dim lvsCustType As Integer = Session("KYC")

        Dim QryIncome = (From tbName In KYCEntity.MCUST_INCOME_SOURCE
                        Where tbName.CUSTSEQ = CustSeq
                       Select tbName.SEQ,
                              tbName.COUNTYNAME,
                              tbName.NATIONCODE,
                              tbName.COUNTYNAMETH,
                              tbName.RISK_LEVEL,
                              ACTIVE = If(tbName.ACTIVE = "Y", True, False)
                     Order By SEQ)

        For Each mRow In QryIncome
            Me.dtIncome.Rows.Add(mRow.COUNTYNAME,
                                 mRow.NATIONCODE,
                                 mRow.COUNTYNAMETH,
                                 mRow.RISK_LEVEL,
                                 mRow.ACTIVE)

            Me.dtIncome.AcceptChanges()
        Next

        Me.grdIncome.DataSource = Me.dtIncome
        Me.grdIncome.DataBind()
        ''''Number of Rows ''''''''''''''''''''''''''''''''''
        Me.lblCount_Inc_Source.Text = Me.dtIncome.Rows.Count
        '''''''''''''''''''''''''''''''''''''''''''''''''''''
        Session("PopINCOME") = Me.dtIncome

    End Sub
    Protected Sub chk_ACT_INC_SOURCE_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        ''''check Session ''''''''''''''''''''''
        If Not (Session("PopINCOME") Is Nothing) Then
            Me.dtIncome = Session("PopINCOME")
        End If

        '''''' assign checkbox value to data table 
        For i = 0 To Me.grdIncome.Rows.Count - 1
            Me.dtIncome.Rows(i).Item("ACTIVE") = CType(Me.grdIncome.Rows(i).Cells(3).FindControl("chk_ACT_INC_SOURCE"), CheckBox).Checked
        Next i

        Me.dtIncome.AcceptChanges()
        Me.grdIncome.DataSource = Me.dtIncome
        Me.grdIncome.DataBind()

        Session("PopINCOME") = Me.dtIncome

    End Sub

#End Region

#Region "เลขที่บัญชีธนาคาร"
    Protected Sub btAddAcc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btAddAcc.Click


        Dim maxSeq As Int32
        Dim cls As New clsAccount
        Dim result As Boolean = True
        Dim intANSWERTYPE As Integer = 0

        If Request.QueryString("KYC") = 1 Then
            intANSWERTYPE = 1
        ElseIf Request.QueryString("KYC") = 2 Then
            intANSWERTYPE = 3
        ElseIf Request.QueryString("KYC") = 3 Then
            intANSWERTYPE = 5
        ElseIf Request.QueryString("KYC") = 4 Then
            intANSWERTYPE = 0
        End If

        If Request.QueryString("KYC") = 1 Then

            If Validate_Account() = False Then Exit Sub

            If cls.chkAccountDisposit(TxtAccNo.Text.Trim) = False Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('เลขที่บัญชีไม่ถูกต้อง');", True)
                result = False
                Me.TxtAccNo.Focus()
                Exit Sub
            End If

        ElseIf Request.QueryString("KYC") = 2 Then

            If Validate_Account() = False Then Exit Sub

            If cls.chkAccountLoan(TxtAccNo.Text.Trim) = False Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('เลขที่บัญชีไม่ถูกต้อง');", True)
                result = False
                Me.TxtAccNo.Focus()
                Exit Sub
            End If
        End If

        If Request.QueryString("KYC") = 1 Or Request.QueryString("KYC") = 3 Then
            If txtAccName.Text = "" Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('เลขที่บัญชีไม่ถูกต้อง');", True)
                result = False
                Me.TxtAccNo.Focus()
                Exit Sub
            Else
                If Not IsNumeric(TxtAccNo.Text) Then
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('เลขที่บัญชีไม่ถูกต้อง');", True)
                    result = False
                    Me.TxtAccNo.Focus()
                    Exit Sub
                End If
            End If
        End If
        '''''''''''''''''''''''''''''''''''''''''''''''

        If ds.Tables("RKYC").Rows.Count = 0 Then
            '' ''maxSeq = 1     เลิกใช้
            If Not ds.Tables("RKYC").Columns.Contains("AccountNO") Then ds.Tables("RKYC").Columns.Add("AccountNO")
            If Not ds.Tables("RKYC").Columns.Contains("ACCOUNTNAME") Then ds.Tables("RKYC").Columns.Add("ACCOUNTNAME")
            If Not ds.Tables("RKYC").Columns.Contains("ANSWERTYPE") Then ds.Tables("RKYC").Columns.Add("ANSWERTYPE")
            If Not ds.Tables("RKYC").Columns.Contains("KYCSEQ") Then ds.Tables("RKYC").Columns.Add("KYCSEQ")
            If Not ds.Tables("RKYC").Columns.Contains("KYCCOMPLETE") Then ds.Tables("RKYC").Columns.Add("KYCCOMPLETE")
            If Not ds.Tables("RKYC").Columns.Contains("APPROVECODE") Then ds.Tables("RKYC").Columns.Add("APPROVECODE")
            If Not ds.Tables("RKYC").Columns.Contains("APPROVENO") Then ds.Tables("RKYC").Columns.Add("APPROVENO")
            If Not ds.Tables("RKYC").Columns.Contains("UPDATEBY") Then ds.Tables("RKYC").Columns.Add("UPDATEBY")
            If Not ds.Tables("RKYC").Columns.Contains("BRANCHCODE") Then ds.Tables("RKYC").Columns.Add("BRANCHCODE")
            If Not ds.Tables("RKYC").Columns.Contains("FLAG_SELF_BO") Then ds.Tables("RKYC").Columns.Add("FLAG_SELF_BO")
            If Not ds.Tables("RKYC").Columns.Contains("FLAG_OTHP_BO") Then ds.Tables("RKYC").Columns.Add("FLAG_OTHP_BO")
            ''''Prachak Nov 10, 2014 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            If Not ds.Tables("RKYC").Columns.Contains("F_JOIN_ACCT") Then ds.Tables("RKYC").Columns.Add("F_JOIN_ACCT")
            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            If Not ds.Tables("RKYC").Columns.Contains("ACTIVE") Then ds.Tables("RKYC").Columns.Add("ACTIVE")
        Else
            ''''เลิกใช้
            '' ''Dim maxRow() As DataRow
            '' ''Dim strQry As String = "KYCSEQ = MAX(KYCSEQ)"
            '' ''maxRow = ds.Tables("RKYC").Select(strQry)
            '' ''maxSeq = maxRow(0)("KYCSEQ") + 1
        End If

        Dim dTable As DataRow = ds.Tables("RKYC").NewRow
        Dim strUserName As String = clsSessionMenuRight.getUserName
        Dim strBranchCode As String = clsSessionMenuRight.getBranchCode

        dTable("AccountNO") = TxtAccNo.Text
        dTable("ACCOUNTNAME") = txtAccName.Text
        dTable("ANSWERTYPE") = intANSWERTYPE    ''''ลอกมาจาก Sub loadKYCAccount()
        '' ''dTable("KYCSEQ") = maxSeq  ''''เลิกใช้
        dTable("KYCSEQ") = -1       ''''ถ้าเป็น record ใหม่ให้เป็น -1
        dTable("KYCCOMPLETE") = "N"
        dTable("APPROVECODE") = 2   ''''1=รออนุมติ,2=อนุมัติทำธุรกรรม,3=ไม่อนุมัติทำธุรกรรม
        dTable("APPROVENO") = ""    ''''txtDApproveNo.Text.Trim
        dTable("UPDATEBY") = strUserName
        dTable("BRANCHCODE") = strBranchCode
        dTable("FLAG_SELF_BO") = False
        dTable("FLAG_OTHP_BO") = False
        dTable("F_JOIN_ACCT") = "S"
        dTable("ACTIVE") = True
        ds.Tables("RKYC").Rows.Add(dTable)

        grdShowAccount.DataSource = ds.Tables("RKYC")
        grdShowAccount.DataBind()
        Me.lblCount_Show_ACCT.Text = ds.Tables("RKYC").Rows.Count
        resetData()
    End Sub

    Function CheckAccount_NO(ByVal lvsAcctno As Object) As Boolean
        Dim cls As New clsAccount
        Dim result As Boolean = True

        If Request.QueryString("KYC") = 1 Then

            If cls.chkAccountDisposit(TxtAccNo.Text.Trim) = False Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('เลขที่บัญชีไม่ถูกต้อง');", True)
                result = False
                lvsAcctno.Focus()

            End If

        ElseIf Request.QueryString("KYC") = 2 Then

            If cls.chkAccountLoan(TxtAccNo.Text.Trim) = False Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('เลขที่บัญชีไม่ถูกต้อง');", True)
                result = False
                lvsAcctno.Focus()

            End If
        End If

        Return result

    End Function

    Function Validate_Account_Before_Add_To_grid() As Boolean

        Dim dtbMCus As DataTable = New DataTable
        Dim lvsSQL As String
        Dim lvsAccount_Name As String = ""
        Dim lvsAleart As String = ""


        Validate_Account_Before_Add_To_grid = False

        If Me.CheckAccount_NO(Me.TxtAccNo) = False Then Exit Function
        If clsValid.CheckNull(Me.txtAccName) = False Then Exit Function


        ''''''''Validate ข้อมูลซ้ำ''''''''''''''''''
        Dim mAccountNo As String = ""
        For Each rowid As GridViewRow In grdShowAccount.Rows

            mAccountNo = rowid.Cells(1).Text
            If mAccountNo = TxtAccNo.Text.Trim Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('ข้อมูลซ้ำ ! กรูณาเลือกบัญชีอื่น');", True)
                TxtAccNo.Focus()
                Exit Function
            End If

        Next

        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

        lvsSQL = ""
        lvsSQL += "     SELECT ACCOUNTNO,                               "
        lvsSQL += "            ACCOUNTNAME                              "
        lvsSQL += "       FROM RKYC                                     "
        lvsSQL += "      WHERE ACCOUNTNO = '" & Me.TxtAccNo.Text & "'   "
        dtbMCus = clsDatabase.QueryDataTable(lvsSQL)

        If (dtbMCus.Rows.Count <> 0) Then

            If Not IsDBNull(dtbMCus.Rows(0)("ACCOUNTNAME")) Then lvsAccount_Name = dtbMCus.Rows(0)("ACCOUNTNAME")
            lvsAleart = "ข้อมูลซ้ำ! กับบัญชีชื่อ -" & lvsAccount_Name & ""
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('" & lvsAleart & "');", True)
            Exit Function
        Else
            Validate_Account_Before_Add_To_grid = True
        End If
        Validate_Account_Before_Add_To_grid = True
    End Function

    Protected Sub chActive_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim rowTable As Int32
        For rowTable = 0 To grdShowAccount.Rows.Count - 1
            With ds.Tables("RKYC")
                .Rows(rowTable).Item("ACTIVE") = CType(grdShowAccount.Rows(rowTable).FindControl("chActive"), CheckBox).Checked
            End With
        Next
        grdShowAccount.DataSource = ds.Tables("RKYC")
        grdShowAccount.DataBind()
    End Sub
    Function Validate_Account() As Boolean

        Validate_Account = False

        If clsValid.CheckNull(Me.txtAccName) = False Then Exit Function


        ''''''''Validate ข้อมูลซ้ำ''''''''''''''''''
        Dim mAccountNo As String = ""
        For Each rowid As GridViewRow In grdShowAccount.Rows

            mAccountNo = rowid.Cells(1).Text
            If mAccountNo = TxtAccNo.Text.Trim Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('ข้อมูลซ้ำ ! กรูณาเลือกบัญชีอื่น');", True)
                TxtAccNo.Focus()
                Exit Function
            End If

        Next

        '''''''''''''''''''''''''''''

        Dim dtbMCus As DataTable = New DataTable
        Dim lvsSQL As String
        Dim lvsAccount_Mame As String = ""
        Dim lvsAleart As String = ""

        lvsSQL = ""
        lvsSQL += "     SELECT ACCOUNTNO,                               "
        lvsSQL += "            ACCOUNTNAME                              "
        lvsSQL += "       FROM RKYC                                     "
        lvsSQL += "      WHERE ACCOUNTNO = '" & Me.TxtAccNo.Text & "'   "
        dtbMCus = clsDatabase.QueryDataTable(lvsSQL)

        If (dtbMCus.Rows.Count <> 0) Then
            lvsAccount_Mame = dtbMCus.Rows(0)("ACCOUNTNAME")
            lvsAleart = "ข้อมูลซ้ำ! กับบัญชีชื่อ -" & lvsAccount_Mame & "- กรูณาเลือกบัญชีอื่น"
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('" & lvsAleart & "');", True)
            Me.TxtAccNo.Focus()
            Exit Function
        Else

        End If
        Validate_Account = True

    End Function

#End Region

#Region "BO"

    Protected Sub grdBo_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles grdBo.SelectedIndexChanging
        indexGrdBoBefore = grdBo.SelectedIndex
    End Sub

    Protected Sub grdBo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdBo.SelectedIndexChanged
        lbSELECTBO.Text = CStr(indexGrdBoBefore)
        If grdBo.SelectedIndex = indexGrdBoBefore Then
            ClearFieldBo()
        Else
            Me.btAddBo.Enabled = True
            lbACCOUNTNO.Text = CType(grdBo.Rows(grdBo.SelectedIndex).FindControl("lbACCOUNTNO_Bo"), Label).Text
            lbCIF_NO.Text = CType(grdBo.Rows(grdBo.SelectedIndex).FindControl("lbCIF_NO_Bo"), Label).Text
            lbUNIQUEID.Text = CType(grdBo.Rows(grdBo.SelectedIndex).FindControl("lbUNIQUEID_Bo"), Label).Text
            lbPASSPORT_NO.Text = CType(grdBo.Rows(grdBo.SelectedIndex).FindControl("lbPASSPORT_NO_Bo"), Label).Text
            lbTHFIRSTNAME.Text = CType(grdBo.Rows(grdBo.SelectedIndex).FindControl("lbTHFIRSTNAME_Bo"), Label).Text
            lbTHMIDNAME.Text = CType(grdBo.Rows(grdBo.SelectedIndex).FindControl("lbTHMIDNAME_Bo"), Label).Text
            lbTHLASTNAME.Text = CType(grdBo.Rows(grdBo.SelectedIndex).FindControl("lbTHLASTNAME_Bo"), Label).Text
            lbACC_OWN_RELATION.Text = CType(grdBo.Rows(grdBo.SelectedIndex).FindControl("lbACC_OWN_RELATION_Bo"), Label).Text
            lbFILTER_RESULT.Text = CType(grdBo.Rows(grdBo.SelectedIndex).FindControl("lbFILTER_RESULT_Bo"), Label).Text
            lbRISK_LEVEL.Text = CType(grdBo.Rows(grdBo.SelectedIndex).FindControl("lbRISK_LEVEL_Bo"), Label).Text
            lbSEQ.Text = CType(grdBo.Rows(grdBo.SelectedIndex).FindControl("lbSEQ_Bo"), Label).Text

            Me.btDelBo.Enabled = True
        End If
    End Sub

    Private Sub ClearFieldBo()
        grdBo.SelectedIndex = -1
        lbACCOUNTNO.Text = ""
        lbCIF_NO.Text = ""
        lbUNIQUEID.Text = ""
        lbPASSPORT_NO.Text = ""
        lbTHFIRSTNAME.Text = ""
        lbTHMIDNAME.Text = ""
        lbTHLASTNAME.Text = ""
        lbACC_OWN_RELATION.Text = ""
        lbRISK_LEVEL.Text = ""
        lbFILTER_RESULT.Text = ""
        lbSEQ.Text = ""
        Me.btDelBo.Enabled = False
    End Sub

    Protected Sub grdShowAccount_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdShowAccount.SelectedIndexChanged

        If (grdShowAccount.SelectedIndex = intBeforeItemIndex) Then
            grdShowAccount.SelectedIndex = -1
            ''''''''''''''''''''''''''''''
            Me.chkBO_SELF.Enabled = False : Me.chkBO_SELF.Checked = False
            Me.chkBO_OTH.Enabled = False : Me.chkBO_OTH.Checked = False

            Me.btDelAcc.Enabled = False

            Me.ddDAcc.Enabled = False
            Me.btAddDAcc.Enabled = False
            Me.btDelDAcc.Enabled = False
            Me.ddDSource.Enabled = False
            Me.btAddDSource.Enabled = False
            Me.btDelDSource.Enabled = False
            'Me.ddLAcc.Enabled = False
            'Me.btAddLAcc.Enabled = False
            'Me.btDelLAcc.Enabled = False

            'Me.ddPAcc.Enabled = False
            'Me.btAddPAcc.Enabled = False
            'Me.btDelPAcc.Enabled = False

            'Me.ddPSource.Enabled = False
            'Me.btAddPSource.Enabled = False
            'Me.btDelPSource.Enabled = False

            Me.lblKYCSEQ.Text = ""

            btAddBo.Text = "เพิ่มข้อมูล BO" : btAddBo.Width = 100
            Me.btAddBo.Enabled = False
            Me.btDelAcc.Enabled = False
            Me.btAddAcc.Enabled = True
            Me.lblKYCSEQ.Text = ""

            ''''''''''''''''''''''''''''''
            resetData()
            btnSave.Visible = False
            btnDeleteAcc.Visible = False
            'btnAdd.Visible = True
            'btnAddOld.Visible = False

        Else
            If TypeKYC = 4 Then
                btnSave.Visible = False
            Else
                btnSave.Visible = True
            End If

            If grdShowAccount.Rows(grdShowAccount.SelectedIndex).Cells(1).Text.Trim = "" Then
                btnDeleteAcc.Visible = True
            Else
                btnDeleteAcc.Visible = False
            End If

            clearPanel()
            Me.btAddBo.Enabled = True
            Dim strKYCSEQ As String = CType(grdShowAccount.Rows(grdShowAccount.SelectedIndex).FindControl("lblKYCSeq"), Label).Text
            Dim strAccName As String = CType(grdShowAccount.Rows(grdShowAccount.SelectedIndex).FindControl("lblAccountName"), Label).Text


            ''''Prachak '''''''''''''
            Dim strF_JOIN_ACCT As String = CType(grdShowAccount.Rows(grdShowAccount.SelectedIndex).FindControl("lblF_JOIN_ACCT"), Label).Text
            Dim strActive As String = CType(grdShowAccount.Rows(grdShowAccount.SelectedIndex).FindControl("chActive"), CheckBox).Checked
            ''''Prachak '''''''''''''
            Me.lblKYCSEQ.Text = strKYCSEQ
            If Me.lblKYCSEQ.Text = "-1" Then Me.btDelAcc.Enabled = True Else Me.btDelAcc.Enabled = True
            Me.TxtAccNo.Enabled = False
            Me.btAddAcc.Enabled = False
            ''''

            Me.chkBO_SELF.Enabled = True
            Me.chkBO_OTH.Enabled = True
            Me.btAddBo.Enabled = True
            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            hdfKYCSeq.Value = strKYCSEQ
            TxtAccNo.Text = grdShowAccount.Rows(grdShowAccount.SelectedIndex).Cells(1).Text.Trim
            If grdShowAccount.Rows(grdShowAccount.SelectedIndex).Cells(1).Text.Trim = "" Then
                txtAccName.Text = Me.txtNameTH.Text
            Else
                txtAccName.Text = strAccName
            End If

            '''' assign value to Active
            If strActive = True Then
                Me.lblACTIVE.Text = "Y"
            Else
                Me.lblACTIVE.Text = "N"
            End If

            If Not TxtAccNo.Text.Trim = "" Then

                If Request.QueryString("KYC") = 1 Then
                    BindingDataKYCDeposit()
                ElseIf Request.QueryString("KYC") = 2 Then
                    BindingDataKYCLoan()
                ElseIf Request.QueryString("KYC") = 3 Then
                    BindingDataKYCPN()

                Else
                    Dim intAnswerType As Integer = CType(grdShowAccount.Rows(grdShowAccount.SelectedIndex).FindControl("lblAnswerType"), Label).Text
                    ' VisbleKYC()
                    If intAnswerType = 1 Or intAnswerType = 2 Then
                        PanelDeposit.Visible = True
                        PanelLoan.Visible = False
                        PanelPN.Visible = False
                        BindingDataKYCDeposit()
                    ElseIf intAnswerType = 3 Or intAnswerType = 4 Then
                        PanelDeposit.Visible = False
                        PanelLoan.Visible = True
                        PanelPN.Visible = False
                        BindingDataKYCLoan()
                    ElseIf intAnswerType = 5 Or intAnswerType = 6 Then
                        PanelDeposit.Visible = False
                        PanelLoan.Visible = False
                        PanelPN.Visible = True
                        BindingDataKYCPN()
                    End If

                End If

            End If

            btAddBo.Text = "เพิ่มข้อมูล BO ของเลขที่บัญชี " & grdShowAccount.Rows(grdShowAccount.SelectedIndex).Cells(1).Text.Trim
            btAddBo.Width = 300
            lbOfAcc1.Text = "ของเลขที่บัญชี " & grdShowAccount.Rows(grdShowAccount.SelectedIndex).Cells(1).Text.Trim
            lbOfAcc2.Text = "ของเลขที่บัญชี " & grdShowAccount.Rows(grdShowAccount.SelectedIndex).Cells(1).Text.Trim
            lbOfAcc3.Text = "ของเลขที่บัญชี " & grdShowAccount.Rows(grdShowAccount.SelectedIndex).Cells(1).Text.Trim

            ''''Prachak ''''''''''''''''''''''''        
            ''''''''''''''''''''''''''''''''''''''
            Me.ddDAcc.Enabled = True
            Me.btAddDAcc.Enabled = True
            Me.btDelDAcc.Enabled = True
            Me.ddDSource.Enabled = True
            Me.btAddDSource.Enabled = True
            Me.btDelDSource.Enabled = True
            'Me.ddLAcc.Enabled = True
            'Me.btAddLAcc.Enabled = True
            'Me.btDelLAcc.Enabled = True
            'Me.ddPAcc.Enabled = True
            'Me.btAddPAcc.Enabled = True
            'Me.btDelPAcc.Enabled = True

            'Me.ddPSource.Enabled = True
            'Me.btAddPSource.Enabled = True
            'Me.btDelPSource.Enabled = True


            chkBO_SELF.Enabled = True
            chkBO_SELF.Checked = CType(grdShowAccount.Rows(grdShowAccount.SelectedIndex).FindControl("lbFLAG_SELF_BO"), Label).Text
            chkBO_OTH.Enabled = True
            chkBO_OTH.Checked = CType(grdShowAccount.Rows(grdShowAccount.SelectedIndex).FindControl("lbFLAG_OTHP_BO"), Label).Text
            btAddBo.Enabled = CType(grdShowAccount.Rows(grdShowAccount.SelectedIndex).FindControl("lbFLAG_OTHP_BO"), Label).Text
        End If

        loadBo()
        If (Request.QueryString("KYC") = 1) Then
            loadDObj(grdDAcc)
            loadDsource(grdDSource)
        ElseIf (Request.QueryString("KYC") = 2) Then
            loadDObj(grdLAcc)
        ElseIf (Request.QueryString("KYC") = 3) Then
            loadDObj(grdPAcc)
            loadDsource(grdPSource)
        End If
        ' VisbleKYC()
    End Sub

    Protected Sub chkObOther_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkBO_OTH.CheckedChanged
        If Trim(TxtAccNo.Text) = "" Then
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", "alert('ต้องเลือกเลขที่บัญชีก่อน')", True)
            Exit Sub
        End If

        If chkBO_OTH.Checked = True Then
            btAddBo.Enabled = True
        Else
            btAddBo.Enabled = False
        End If

        Dim dTable() As DataRow
        Dim strQry As String = "AccountNO = '" & grdShowAccount.Rows(grdShowAccount.SelectedIndex).Cells(1).Text & "'"
        dTable = ds.Tables("RKYC").Select(strQry)
        dTable(0)("FLAG_OTHP_BO") = chkBO_OTH.Checked
        grdShowAccount.DataSource = ds.Tables("RKYC")
        grdShowAccount.DataBind()
    End Sub

    Private Sub loadBo()

        Dim clsDataTable As New clsLINQToDataTable
        Dim CUSTTYPE As Int32 = Request.QueryString("KYC")

        If ds.Tables.Contains("MCUST_BO_ACCT") Then
            ds.Tables.Remove("MCUST_BO_ACCT")
        End If

        Dim qry = From tbl In KYCEntity.MCUST_BO_ACCT
                 Where tbl.CUSTSEQ = CustSeq And tbl.ACCOUNTNO = TxtAccNo.Text
              Order By tbl.SEQ
                Select tbl.ACCOUNTNO,
                       tbl.CIF_NO,
                       tbl.UNIQUEID,
                       tbl.PASSPORT_NO,
                       tbl.THFIRSTNAME,
                       tbl.THMIDNAME,
                       tbl.THLASTNAME,
                       tbl.ACC_OWN_RELATION,
                       tbl.RISK_LEVEL,
                       tbl.FILTER_RESULT,
                       tbl.SEQ,
                       ACTIVE = If(tbl.ACTIVE = "Y", True, False)

        ds.Tables.Add(clsDataTable.LINQToDataTable(qry, "MCUST_BO_ACCT").Copy)

        grdBo.DataSource = ds.Tables("MCUST_BO_ACCT")
        grdBo.DataBind()
        Me.lblCount_grdBo.Text = ds.Tables("MCUST_BO_ACCT").Rows.Count

    End Sub

    Protected Sub chActiveBO_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim rowTable As Int32
        For rowTable = 0 To grdBo.Rows.Count - 1
            With ds.Tables("MCUST_BO_ACCT")
                .Rows(rowTable).Item("ACTIVE") = CType(grdBo.Rows(rowTable).FindControl("chActiveBO"), CheckBox).Checked
            End With
        Next
        grdBo.DataSource = ds.Tables("MCUST_BO_ACCT")
        grdBo.DataBind()
        Me.lblCount_grdBo.Text = ds.Tables("MCUST_BO_ACCT").Rows.Count
    End Sub

    Protected Sub btAddBo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btAddBo.Click
        Try

            If Trim(TxtAccNo.Text) = "" Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", "alert('ต้องเลือกเลขที่บัญชีก่อนทำการเพิ่ม BO')", True)
                Exit Sub
            End If
            ClearFieldBo()
            grdBo.DataSource = ds.Tables("MCUST_BO_ACCT")
            grdBo.DataBind()
            Me.lblCount_grdBo.Text = ds.Tables("MCUST_BO_ACCT").Rows.Count
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(Page, _
                                                            Page.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

#End Region

#Region "Save By New Method"

    Protected Sub Save_to_MCUST_BO_Deposit(ByVal key As Integer, ByVal acctNo As String)

        Dim mBO_Acctno As String = ""
        Dim mBO_CIF_NO As String = ""
        Dim mBO_UNIQUEID As String = ""
        Dim mBO_PASSPORT_NO As String = ""
        Dim mBO_THFIRSTNAME As String = ""
        Dim mBO_THMIDNAME As String = ""
        Dim mBO_THLASTNAME As String = ""
        Dim mBO_ACC_OWN_RELATION As String = ""
        Dim mBO_FILTER_RESULT As String = ""
        Dim mBO_RISK_LEVEL As String = ""
        Dim chActive As String = ""
        Dim lvsCustType As Integer = Session("KYC")
        '''''''''''''''''''''''
        Try

            Dim DeletedQuery = (From m In KYCEntity.MCUST_BO_ACCT
                               Where m.CUSTSEQ = key _
                                 And m.ACCOUNTNO = TxtAccNo.Text _
                                 And m.CUSTTYPE = lvsCustType
                              Select m).ToList

            If DeletedQuery.Count > 0 Then
                For i As Integer = 0 To DeletedQuery.Count - 1
                    KYCEntity.MCUST_BO_ACCT.DeleteObject(DeletedQuery(i))
                Next i
            End If
            ''''''''''''''''''''''
            For Each lvsRow_No As GridViewRow In Me.grdBo.Rows
                Dim newCust_BO As New MCUST_BO_ACCT

                ''''get values from Gridview '''''''
                mBO_Acctno = CType(lvsRow_No.FindControl("lbACCOUNTNO_Bo"), Label).Text
                mBO_CIF_NO = CType(lvsRow_No.FindControl("lbCIF_NO_Bo"), Label).Text
                mBO_UNIQUEID = CType(lvsRow_No.FindControl("lbUNIQUEID_Bo"), Label).Text
                mBO_PASSPORT_NO = CType(lvsRow_No.FindControl("lbPASSPORT_NO_Bo"), Label).Text
                mBO_THFIRSTNAME = CType(lvsRow_No.FindControl("lbTHFIRSTNAME_Bo"), Label).Text
                mBO_THMIDNAME = CType(lvsRow_No.FindControl("lbTHMIDNAME_Bo"), Label).Text
                mBO_THLASTNAME = CType(lvsRow_No.FindControl("lbTHLASTNAME_Bo"), Label).Text
                mBO_ACC_OWN_RELATION = CType(lvsRow_No.FindControl("lbACC_OWN_RELATION_Bo"), Label).Text
                mBO_FILTER_RESULT = CType(lvsRow_No.FindControl("lbFILTER_RESULT_Bo"), Label).Text
                mBO_RISK_LEVEL = CType(lvsRow_No.FindControl("lbRISK_LEVEL_Bo"), Label).Text
                If (CType(lvsRow_No.Cells(8).FindControl("chActiveBO"), CheckBox).Checked = True) Then
                    chActive = "Y"
                Else
                    chActive = "N"
                End If

                ''''''''''''''''''''''''''''''''''''

                '''''''LINQ Start here '''''''''''''
                newCust_BO.CUSTSEQ = key
                newCust_BO.CUSTTYPE = Session("KYC")
                newCust_BO.SEQ = lvsRow_No.RowIndex + 1
                newCust_BO.ACCOUNTNO = mBO_Acctno.Trim
                newCust_BO.CIF_NO = mBO_CIF_NO.Trim
                newCust_BO.UNIQUEID = mBO_UNIQUEID.Trim
                newCust_BO.PASSPORT_NO = mBO_PASSPORT_NO.Trim
                newCust_BO.THFIRSTNAME = mBO_THFIRSTNAME.Trim
                newCust_BO.THMIDNAME = mBO_THMIDNAME.Trim
                newCust_BO.THLASTNAME = mBO_THLASTNAME.Trim
                newCust_BO.ACC_OWN_RELATION = mBO_ACC_OWN_RELATION.Trim
                newCust_BO.FILTER_RESULT = mBO_FILTER_RESULT.Trim
                newCust_BO.RISK_LEVEL = CInt(mBO_RISK_LEVEL.Trim)
                newCust_BO.ACTIVE = chActive
                newCust_BO.CREATED_DATE = Now
                newCust_BO.CREATED_BY = clsSessionMenuRight.getUserName
                KYCEntity.AddToMCUST_BO_ACCT(newCust_BO)

            Next
            KYCEntity.SaveChanges()

        Catch ex As Exception
            Throw ex

        End Try
    End Sub

#End Region


#Region "สอบถามเงินฝาก"

    Function Validate_Deposit_Obj() As Boolean
        Validate_Deposit_Obj = False

        ''''''''Validate ข้อมูลซ้ำ''''''''''''''''''
        Dim mObjective As String = ""
        For Each rowIncome As GridViewRow In Me.grdDAcc.Rows
            mObjective = rowIncome.Cells(2).Text
            If mObjective = ddDAcc.SelectedItem.Text Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('ข้อมูลซ้ำ ! กรูณาเลือกวัตถุประสงค์ใหม่');", True)
                ddDAcc.Focus()
                Exit Function
            End If
        Next
        '''''''''''''''''''''''''''''
        Validate_Deposit_Obj = True
    End Function

    Protected Sub btAddDAcc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btAddDAcc.Click
        Add_To_Grid_grdDAcc()
    End Sub

    Sub Add_To_Grid_grdDAcc()

        Try
            If Trim(TxtAccNo.Text) = "" Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", "alert('ต้องเลือกเลขที่บัญชีก่อนทำการเพิ่มข้อมูล')", True)
                Exit Sub
            End If

            If Validate_Deposit_Obj() = False Then Exit Sub

            Dim maxSeq As Int32
            If ds.Tables("MCUST_DEPOSIT_OBJ").Rows.Count = 0 Then
                maxSeq = 1
                If Not ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Contains("ACCOUNTNO") Then ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Add("ACCOUNTNO")
                If Not ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Contains("OBJECTIVE") Then ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Add("OBJECTIVE")
                If Not ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Contains("REMARKS") Then ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Add("REMARKS")
                If Not ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Contains("ACTIVE") Then ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Add("ACTIVE")
                If Not ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Contains("SEQ") Then ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Add("SEQ")
            Else
                Dim maxRow() As DataRow
                Dim strQry As String = "SEQ = MAX(SEQ)"
                maxRow = ds.Tables("MCUST_DEPOSIT_OBJ").Select(strQry)
                maxSeq = maxRow(0)("SEQ") + 1
            End If
            Dim dTable As DataRow = ds.Tables("MCUST_DEPOSIT_OBJ").NewRow

            dTable("ACCOUNTNO") = TxtAccNo.Text
            dTable("OBJECTIVE") = ddDAcc.SelectedItem
            dTable("REMARKS") = txtDAccOther.Text
            dTable("SEQ") = maxSeq
            dTable("ACTIVE") = True
            ds.Tables("MCUST_DEPOSIT_OBJ").Rows.Add(dTable)

            grdDAcc.DataSource = ds.Tables("MCUST_DEPOSIT_OBJ")
            grdDAcc.DataBind()
            clearDAcc()
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(Page, _
                                                            Page.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub btDelDAcc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btDelDAcc.Click
        Delete_gridRow_grdDAcc()
    End Sub

    Sub Delete_gridRow_grdDAcc()
        Try

            Dim index As Integer = grdDAcc.SelectedIndex
            If index = -1 Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", "alert('ต้องเลือกวัตถุประสงค์ของการเปิดบัญชีที่ต้องการลบก่อน')", True)
                Exit Sub
            Else
                ds.Tables("MCUST_DEPOSIT_OBJ").Rows(grdDAcc.SelectedIndex).Delete()
            End If

            grdDAcc.DataSource = ds.Tables("MCUST_DEPOSIT_OBJ")
            grdDAcc.DataBind()
            clearDAcc()

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(Page, _
                                                            Page.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Private Sub loadDObj(ByVal grdObj As GridView)

        Dim clsDataTable As New clsLINQToDataTable
        Dim CUSTTYPE As Int32 = Request.QueryString("KYC")

        If ds.Tables.Contains("MCUST_DEPOSIT_OBJ") Then
            ds.Tables.Remove("MCUST_DEPOSIT_OBJ")
        End If

        Dim qry = From tbl In KYCEntity.MCUST_DEPOSIT_OBJ
                 Where tbl.CUSTSEQ = CustSeq And tbl.ACCOUNTNO = TxtAccNo.Text And tbl.CUSTTYPE = CUSTTYPE
              Order By tbl.SEQ
                Select tbl.ACCOUNTNO,
                       tbl.OBJECTIVE,
                       tbl.REMARKS,
                       tbl.SEQ,
                       ACTIVE = If(tbl.ACTIVE = "Y", True, False)

        ds.Tables.Add(clsDataTable.LINQToDataTable(qry, "MCUST_DEPOSIT_OBJ").Copy)

        grdObj.DataSource = ds.Tables("MCUST_DEPOSIT_OBJ")
        grdObj.DataBind()
    End Sub

    Protected Sub chActiveDAcc_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim rowTable As Int32
        For rowTable = 0 To grdDAcc.Rows.Count - 1
            With ds.Tables("MCUST_DEPOSIT_OBJ")
                .Rows(rowTable).Item("ACTIVE") = CType(grdDAcc.Rows(rowTable).FindControl("chActiveDAcc"), CheckBox).Checked
            End With
        Next
        grdDAcc.DataSource = ds.Tables("MCUST_DEPOSIT_OBJ")
        grdDAcc.DataBind()
    End Sub

    Protected Sub Save_to_MCUST_OBJ_Deposit(ByVal key As Integer, ByVal acctNo As String)

        Dim mDO_Acctno As String = ""
        Dim mDO_Objective As String = ""
        Dim mDO_Remarks As String = ""
        Dim chActive As String = ""
        Dim lvsCustType As Integer = Session("KYC")
        '''''''''''''''''''''''
        If lvsCustType <> 1 Then Exit Sub
        Try

            Dim DeletedQuery = (From m In KYCEntity.MCUST_DEPOSIT_OBJ
                               Where m.CUSTSEQ = key _
                                 And m.ACCOUNTNO = acctNo _
                                 And m.CUSTTYPE = lvsCustType
                              Select m).ToList

            If DeletedQuery.Count > 0 Then
                For i As Integer = 0 To DeletedQuery.Count - 1
                    KYCEntity.MCUST_DEPOSIT_OBJ.DeleteObject(DeletedQuery(i))
                Next i
            End If
            ''''''''''''''''''''''
            For Each lvsRow_No As GridViewRow In Me.grdDAcc.Rows
                Dim newCust_Dept_Obj As New MCUST_DEPOSIT_OBJ

                ''''get values from Gridview '''''''

                mDO_Acctno = lvsRow_No.Cells(0).Text : If mDO_Acctno = "&nbsp;" Then mDO_Acctno = ""
                mDO_Objective = lvsRow_No.Cells(2).Text : If mDO_Objective = "&nbsp;" Then mDO_Objective = ""
                mDO_Remarks = lvsRow_No.Cells(3).Text : If mDO_Remarks = "&nbsp;" Then mDO_Remarks = ""
                If (CType(lvsRow_No.Cells(4).FindControl("chActiveDAcc"), CheckBox).Checked = True) Then
                    chActive = "Y"
                Else
                    chActive = "N"
                End If

                ''''''''''''''''''''''''''''''''''''

                '''''''LINQ Start here '''''''''''''
                newCust_Dept_Obj.CUSTSEQ = key
                newCust_Dept_Obj.CUSTTYPE = Session("KYC")
                newCust_Dept_Obj.SEQ = lvsRow_No.RowIndex + 1
                newCust_Dept_Obj.ACCOUNTNO = mDO_Acctno.Trim
                newCust_Dept_Obj.OBJECTIVE = mDO_Objective.Trim
                newCust_Dept_Obj.REMARKS = mDO_Remarks.Trim
                newCust_Dept_Obj.ACTIVE = chActive
                newCust_Dept_Obj.CREATED_DATE = Now
                newCust_Dept_Obj.CREATED_BY = clsSessionMenuRight.getUserName
                KYCEntity.AddToMCUST_DEPOSIT_OBJ(newCust_Dept_Obj)

            Next
            KYCEntity.SaveChanges()

        Catch ex As Exception
            Throw ex

        End Try
    End Sub

    Protected Sub grdDAcc_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdDAcc.SelectedIndexChanged
        If indexGrdDAccBefore = grdDAcc.SelectedIndex Then
            grdDAcc.SelectedIndex = -1
        End If
    End Sub

    Protected Sub grdDAcc_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles grdDAcc.SelectedIndexChanging
        indexGrdDAccBefore = grdDAcc.SelectedIndex
    End Sub

    Private Sub clearDAcc()
        grdDAcc.SelectedIndex = -1
        ddDAcc.SelectedIndex = -1
        txtDAccOther.Text = ""
    End Sub

#End Region

#Region "แหล่งที่มาของเงินฝาก"

    Function Validate_Deposit_Source() As Boolean

        Validate_Deposit_Source = False

        ''''''''Validate ข้อมูลซ้ำ''''''''''''''''''
        Dim mObjective As String = ""
        For Each rowIncome As GridViewRow In Me.grdDSource.Rows
            mObjective = rowIncome.Cells(2).Text
            If mObjective = ddDSource.SelectedItem.Text Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('ข้อมูลซ้ำ ! กรูณาเลือกแหล่งที่มาของเงินฝากใหม่');", True)
                ddDSource.Focus()
                Exit Function
            End If
        Next
        '''''''''''''''''''''''''''''
        Validate_Deposit_Source = True
    End Function

    Protected Sub btAddDSource_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btAddDSource.Click
        Me.Add_To_grid_DSource()
    End Sub

    Sub Add_To_grid_DSource()

        Try

            If Me.Validate_Deposit_Source() = False Then Exit Sub

            If Trim(TxtAccNo.Text) = "" Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", "alert('ต้องเลือกเลขที่บัญชีก่อนทำการเพิ่มข้อมูล')", True)
                Exit Sub
            End If

            Dim maxSeq As Int32
            If ds.Tables("MCUST_DEPOSIT_SOURCE").Rows.Count = 0 Then
                maxSeq = 1
                If Not ds.Tables("MCUST_DEPOSIT_SOURCE").Columns.Contains("ACCOUNTNO") Then ds.Tables("MCUST_DEPOSIT_SOURCE").Columns.Add("ACCOUNTNO")
                If Not ds.Tables("MCUST_DEPOSIT_SOURCE").Columns.Contains("SOURCE_OF_M") Then ds.Tables("MCUST_DEPOSIT_SOURCE").Columns.Add("SOURCE_OF_M")
                If Not ds.Tables("MCUST_DEPOSIT_SOURCE").Columns.Contains("REMARKS") Then ds.Tables("MCUST_DEPOSIT_SOURCE").Columns.Add("REMARKS")
                If Not ds.Tables("MCUST_DEPOSIT_SOURCE").Columns.Contains("ACTIVE") Then ds.Tables("MCUST_DEPOSIT_SOURCE").Columns.Add("ACTIVE")
                If Not ds.Tables("MCUST_DEPOSIT_SOURCE").Columns.Contains("SEQ") Then ds.Tables("MCUST_DEPOSIT_SOURCE").Columns.Add("SEQ")
            Else
                Dim maxRow() As DataRow
                Dim strQry As String = "SEQ = MAX(SEQ)"
                maxRow = ds.Tables("MCUST_DEPOSIT_SOURCE").Select(strQry)
                maxSeq = maxRow(0)("SEQ") + 1
            End If
            Dim dTable As DataRow = ds.Tables("MCUST_DEPOSIT_SOURCE").NewRow

            dTable("ACCOUNTNO") = TxtAccNo.Text
            dTable("SOURCE_OF_M") = ddDSource.SelectedItem
            dTable("REMARKS") = txtDSourceOther.Text
            dTable("SEQ") = maxSeq
            dTable("ACTIVE") = True
            ds.Tables("MCUST_DEPOSIT_SOURCE").Rows.Add(dTable)

            grdDSource.DataSource = ds.Tables("MCUST_DEPOSIT_SOURCE")
            grdDSource.DataBind()
            clearDSource()
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(Page, _
                                                            Page.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub btDelDSource_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btDelDSource.Click
        Delete_gridRow_DSource()
    End Sub

    Sub Delete_gridRow_DSource()
        Try

            Dim index As Integer = grdDSource.SelectedIndex
            If index = -1 Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", "alert('ต้องเลือกแหล่งที่มาของเงินฝากที่ต้องการลบก่อน')", True)
                Exit Sub
            Else
                ds.Tables("MCUST_DEPOSIT_SOURCE").Rows(grdDSource.SelectedIndex).Delete()
            End If
            clearDSource()
            grdDSource.DataSource = ds.Tables("MCUST_DEPOSIT_SOURCE")
            grdDSource.DataBind()
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(Page, _
                                                            Page.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Private Sub loadDsource(ByVal grdObj As GridView)

        Dim clsDataTable As New clsLINQToDataTable
        Dim CUSTTYPE As Int32 = Request.QueryString("KYC")

        If ds.Tables.Contains("MCUST_DEPOSIT_SOURCE") Then
            ds.Tables.Remove("MCUST_DEPOSIT_SOURCE")
        End If

        Dim qry = From tbl In KYCEntity.MCUST_DEPOSIT_SOURCE
                 Where tbl.CUSTSEQ = CustSeq And tbl.ACCOUNTNO = TxtAccNo.Text And tbl.CUSTTYPE = CUSTTYPE
              Order By tbl.SEQ
                Select tbl.ACCOUNTNO,
                       tbl.SOURCE_OF_M,
                       tbl.REMARKS,
                       tbl.SEQ,
                       ACTIVE = If(tbl.ACTIVE = "Y", True, False)

        ds.Tables.Add(clsDataTable.LINQToDataTable(qry, "MCUST_DEPOSIT_SOURCE").Copy)

        grdObj.DataSource = ds.Tables("MCUST_DEPOSIT_SOURCE")
        grdObj.DataBind()
    End Sub

    Protected Sub chActiveDSource_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim rowTable As Int32
        For rowTable = 0 To grdDSource.Rows.Count - 1
            With ds.Tables("MCUST_DEPOSIT_SOURCE")
                .Rows(rowTable).Item("ACTIVE") = CType(grdDSource.Rows(rowTable).FindControl("chActiveDSource"), CheckBox).Checked
            End With
        Next
        grdDSource.DataSource = ds.Tables("MCUST_DEPOSIT_SOURCE")
        grdDSource.DataBind()
    End Sub

    Protected Sub Save_to_MCUST_SOURCE_Deposit(ByVal key As Integer, ByVal acctNo As String)

        Dim mDS_Acctno As String = ""
        Dim mDS_Source As String = ""
        Dim mDS_Remarks As String = ""
        Dim chActive As String = ""
        Dim lvsCustType As Integer = Session("KYC")
        '''''''''''''''''''''''
        If lvsCustType <> 1 Then Exit Sub
        Try

            Dim DeletedQuery = (From m In KYCEntity.MCUST_DEPOSIT_SOURCE
                               Where m.CUSTSEQ = key _
                                 And m.ACCOUNTNO = acctNo _
                                 And m.CUSTTYPE = lvsCustType
                              Select m).ToList

            If DeletedQuery.Count > 0 Then
                For i As Integer = 0 To DeletedQuery.Count - 1
                    KYCEntity.MCUST_DEPOSIT_SOURCE.DeleteObject(DeletedQuery(i))
                Next i
            End If
            ''''''''''''''''''''''
            For Each lvsRow_No As GridViewRow In Me.grdDSource.Rows
                Dim newCust_Dept_Source As New MCUST_DEPOSIT_SOURCE

                ''''get values from Gridview '''''''

                mDS_Acctno = lvsRow_No.Cells(0).Text : If mDS_Acctno = "&nbsp;" Then mDS_Acctno = ""
                mDS_Source = lvsRow_No.Cells(2).Text : If mDS_Source = "&nbsp;" Then mDS_Source = ""
                mDS_Remarks = lvsRow_No.Cells(3).Text : If mDS_Remarks = "&nbsp;" Then mDS_Remarks = ""
                If (CType(lvsRow_No.Cells(4).FindControl("chActiveDSource"), CheckBox).Checked = True) Then
                    chActive = "Y"
                Else
                    chActive = "N"
                End If

                ''''''''''''''''''''''''''''''''''''

                '''''''LINQ Start here '''''''''''''
                newCust_Dept_Source.CUSTSEQ = key
                newCust_Dept_Source.CUSTTYPE = Session("KYC")
                newCust_Dept_Source.SEQ = lvsRow_No.RowIndex + 1
                newCust_Dept_Source.ACCOUNTNO = mDS_Acctno.Trim
                newCust_Dept_Source.SOURCE_OF_M = mDS_Source.Trim
                newCust_Dept_Source.REMARKS = mDS_Remarks.Trim
                newCust_Dept_Source.ACTIVE = chActive
                newCust_Dept_Source.CREATED_DATE = Now
                newCust_Dept_Source.CREATED_BY = clsSessionMenuRight.getUserName
                KYCEntity.AddToMCUST_DEPOSIT_SOURCE(newCust_Dept_Source)

            Next
            KYCEntity.SaveChanges()

        Catch ex As Exception
            Throw ex

        End Try
    End Sub

    Protected Sub grdDSource_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdDSource.SelectedIndexChanged
        If indexGrdDSourceBefore = grdDSource.SelectedIndex Then
            grdDSource.SelectedIndex = -1
        End If
    End Sub

    Protected Sub grdDSource_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles grdDSource.SelectedIndexChanging
        indexGrdDSourceBefore = grdDSource.SelectedIndex
    End Sub

    Private Sub clearDSource()
        grdDSource.SelectedIndex = -1
        ddDSource.SelectedIndex = -1
        txtDSourceOther.Text = ""
    End Sub

#End Region
#Region "สอบถามสินเชื่อ"

    Function Validate_Row_53() As Boolean
        Validate_Row_53 = False

        ''''''''Validate ข้อมูลซ้ำ''''''''''''''''''
        Dim mIncome As String = ""
        For Each rowID As GridViewRow In grdLAcc.Rows
            mIncome = rowID.Cells(2).Text
            If mIncome = ddLAcc.SelectedItem.Text Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('ข้อมูลซ้ำ ! กรูณาเลือกแหล่งที่มาของรายได้อื่น');", True)
                ddLAcc.Focus()
                Exit Function
            End If

        Next
        '''''''''''''''''''''''''''''
        Validate_Row_53 = True
    End Function

    Protected Sub btAddLAcc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btAddLAcc.Click
        Me.Add_To_Grid_LAcc()
    End Sub

    Sub Add_To_Grid_LAcc()
        Try
            If Trim(TxtAccNo.Text) = "" Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", "alert('ต้องเลือกเลขที่บัญชีก่อนทำการเพิ่มข้อมูล')", True)
                Exit Sub
            End If

            If Me.Validate_Row_53 = False Then Exit Sub

            Dim maxSeq As Int32
            If ds.Tables("MCUST_DEPOSIT_OBJ").Rows.Count = 0 Then
                maxSeq = 1
                If Not ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Contains("ACCOUNTNO") Then ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Add("ACCOUNTNO")
                If Not ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Contains("OBJECTIVE") Then ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Add("OBJECTIVE")
                If Not ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Contains("REMARKS") Then ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Add("REMARKS")
                If Not ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Contains("ACTIVE") Then ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Add("ACTIVE")
                If Not ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Contains("SEQ") Then ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Add("SEQ")
            Else
                Dim maxRow() As DataRow
                Dim strQry As String = "SEQ = MAX(SEQ)"
                maxRow = ds.Tables("MCUST_DEPOSIT_OBJ").Select(strQry)
                maxSeq = maxRow(0)("SEQ") + 1
            End If
            Dim dTable As DataRow = ds.Tables("MCUST_DEPOSIT_OBJ").NewRow

            dTable("ACCOUNTNO") = TxtAccNo.Text
            dTable("OBJECTIVE") = ddLAcc.SelectedItem
            dTable("REMARKS") = txtLAccOther.Text
            dTable("SEQ") = maxSeq
            dTable("ACTIVE") = True
            ds.Tables("MCUST_DEPOSIT_OBJ").Rows.Add(dTable)

            grdLAcc.DataSource = ds.Tables("MCUST_DEPOSIT_OBJ")
            grdLAcc.DataBind()
            clearLAcc()
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(Page, _
                                                            Page.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try

    End Sub

    Protected Sub btDelLAcc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btDelLAcc.Click
        Me.Delete_GridRow_LAcc()
    End Sub

    Sub Delete_GridRow_LAcc()
        Try

            Dim index As Integer = grdLAcc.SelectedIndex
            If index = -1 Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", "alert('ต้องเลือกวัตถุประสงค์ของบัญชีสินเชื่อที่ต้องการลบก่อน')", True)
                Exit Sub
            Else
                ds.Tables("MCUST_DEPOSIT_OBJ").Rows(grdLAcc.SelectedIndex).Delete()
            End If
            grdLAcc.DataSource = ds.Tables("MCUST_DEPOSIT_OBJ")
            grdLAcc.DataBind()
            clearLAcc()
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(Page, _
                                                            Page.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try

    End Sub

    Protected Sub chActiveLAcc_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim rowTable As Int32
        For rowTable = 0 To grdLAcc.Rows.Count - 1
            With ds.Tables("MCUST_DEPOSIT_OBJ")
                .Rows(rowTable).Item("ACTIVE") = CType(grdLAcc.Rows(rowTable).FindControl("chActiveLAcc"), CheckBox).Checked
            End With
        Next
        grdLAcc.DataSource = ds.Tables("MCUST_DEPOSIT_OBJ")
        grdLAcc.DataBind()
    End Sub

    Protected Sub Save_to_MCUST_OBJ_Loan(ByVal key As Integer, ByVal acctNo As String)

        Dim mDO_Acctno As String = ""
        Dim mDO_Objective As String = ""
        Dim mDO_Remarks As String = ""
        Dim chActive As String = ""
        Dim lvsCustType As Integer = Session("KYC")
        '''''''''''''''''''''''
        If lvsCustType <> 2 Then Exit Sub
        Try

            Dim DeletedQuery = (From m In KYCEntity.MCUST_DEPOSIT_OBJ
                               Where m.CUSTSEQ = key _
                                 And m.ACCOUNTNO = acctNo _
                                 And m.CUSTTYPE = lvsCustType
                              Select m).ToList

            If DeletedQuery.Count > 0 Then
                For i As Integer = 0 To DeletedQuery.Count - 1
                    KYCEntity.MCUST_DEPOSIT_OBJ.DeleteObject(DeletedQuery(i))
                Next i
            End If
            ''''''''''''''''''''''
            For Each lvsRow_No As GridViewRow In Me.grdLAcc.Rows
                Dim newCust_Dept_Obj As New MCUST_DEPOSIT_OBJ

                ''''get values from Gridview '''''''

                mDO_Acctno = lvsRow_No.Cells(0).Text : If mDO_Acctno = "&nbsp;" Then mDO_Acctno = ""
                mDO_Objective = lvsRow_No.Cells(2).Text : If mDO_Objective = "&nbsp;" Then mDO_Objective = ""
                mDO_Remarks = lvsRow_No.Cells(3).Text : If mDO_Remarks = "&nbsp;" Then mDO_Remarks = ""
                If (CType(lvsRow_No.Cells(4).FindControl("chActiveLAcc"), CheckBox).Checked = True) Then
                    chActive = "Y"
                Else
                    chActive = "N"
                End If

                ''''''''''''''''''''''''''''''''''''

                '''''''LINQ Start here '''''''''''''
                newCust_Dept_Obj.CUSTSEQ = key
                newCust_Dept_Obj.CUSTTYPE = Session("KYC")
                newCust_Dept_Obj.SEQ = lvsRow_No.RowIndex + 1
                newCust_Dept_Obj.ACCOUNTNO = mDO_Acctno.Trim
                newCust_Dept_Obj.OBJECTIVE = mDO_Objective.Trim
                newCust_Dept_Obj.REMARKS = mDO_Remarks.Trim
                newCust_Dept_Obj.ACTIVE = chActive
                newCust_Dept_Obj.CREATED_DATE = Now
                newCust_Dept_Obj.CREATED_BY = clsSessionMenuRight.getUserName
                KYCEntity.AddToMCUST_DEPOSIT_OBJ(newCust_Dept_Obj)

            Next
            KYCEntity.SaveChanges()

        Catch ex As Exception
            Throw ex

        End Try
    End Sub

    Protected Sub grdLAcc_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdLAcc.SelectedIndexChanged
        If indexGrdLAccBefore = grdLAcc.SelectedIndex Then
            grdLAcc.SelectedIndex = -1
        End If
    End Sub

    Protected Sub grdLAcc_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles grdLAcc.SelectedIndexChanging
        indexGrdLAccBefore = grdLAcc.SelectedIndex
    End Sub

    Private Sub clearLAcc()
        grdLAcc.SelectedIndex = -1
        ddLAcc.SelectedIndex = -1
        txtLAccOther.Text = ""
    End Sub

#End Region
#Region "สอบถามตั๋วเงิน"

    Protected Sub btAddPAcc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btAddPAcc.Click
        Me.Add_To_Grid_PAcc()
    End Sub

    Sub Add_To_Grid_PAcc()
        Try
            If Trim(TxtAccNo.Text) = "" Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", "alert('ต้องเลือกเลขที่บัญชีก่อนทำการเพิ่มข้อมูล')", True)
                Exit Sub
            End If

            Dim maxSeq As Int32
            If ds.Tables("MCUST_DEPOSIT_OBJ").Rows.Count = 0 Then
                maxSeq = 1
                If Not ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Contains("ACCOUNTNO") Then ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Add("ACCOUNTNO")
                If Not ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Contains("OBJECTIVE") Then ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Add("OBJECTIVE")
                If Not ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Contains("REMARKS") Then ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Add("REMARKS")
                If Not ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Contains("ACTIVE") Then ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Add("ACTIVE")
                If Not ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Contains("SEQ") Then ds.Tables("MCUST_DEPOSIT_OBJ").Columns.Add("SEQ")
            Else
                Dim maxRow() As DataRow
                Dim strQry As String = "SEQ = MAX(SEQ)"
                maxRow = ds.Tables("MCUST_DEPOSIT_OBJ").Select(strQry)
                maxSeq = maxRow(0)("SEQ") + 1
            End If
            Dim dTable As DataRow = ds.Tables("MCUST_DEPOSIT_OBJ").NewRow

            dTable("ACCOUNTNO") = TxtAccNo.Text
            dTable("OBJECTIVE") = ddPAcc.SelectedItem
            dTable("REMARKS") = txtPAccOther.Text
            dTable("SEQ") = maxSeq
            dTable("ACTIVE") = True
            ds.Tables("MCUST_DEPOSIT_OBJ").Rows.Add(dTable)

            grdPAcc.DataSource = ds.Tables("MCUST_DEPOSIT_OBJ")
            grdPAcc.DataBind()
            clearPAcc()
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(Page, _
                                                            Page.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub btDelPAcc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btDelPAcc.Click
        Delete_Grid_row_PSource()
    End Sub

    Sub Delete_Grid_row_PSource()
        Try

            Dim index As Integer = grdPAcc.SelectedIndex
            If index = -1 Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", "alert('ต้องเลือกวัตถุประสงค์ของการเปิดบัญชีที่ต้องการลบก่อน')", True)
                Exit Sub
            Else
                ds.Tables("MCUST_DEPOSIT_OBJ").Rows(grdPAcc.SelectedIndex).Delete()
            End If

            grdPAcc.DataSource = ds.Tables("MCUST_DEPOSIT_OBJ")
            grdPAcc.DataBind()
            clearPAcc()

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(Page, _
                                                            Page.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub chActivePAcc_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim rowTable As Int32
        For rowTable = 0 To grdPAcc.Rows.Count - 1
            With ds.Tables("MCUST_DEPOSIT_OBJ")
                .Rows(rowTable).Item("ACTIVE") = CType(grdPAcc.Rows(rowTable).FindControl("chActivePAcc"), CheckBox).Checked
            End With
        Next
        grdPAcc.DataSource = ds.Tables("MCUST_DEPOSIT_OBJ")
        grdPAcc.DataBind()
    End Sub

    Protected Sub Save_to_MCUST_OBJ_PN(ByVal key As Integer, ByVal acctNo As String)

        Dim mDO_Acctno As String = ""
        Dim mDO_Objective As String = ""
        Dim mDO_Remarks As String = ""
        Dim chActive As String = ""
        Dim lvsCustType As Integer = Session("KYC")
        '''''''''''''''''''''''
        If lvsCustType <> 3 Then Exit Sub

        Try

            Dim DeletedQuery = (From m In KYCEntity.MCUST_DEPOSIT_OBJ
                               Where m.CUSTSEQ = key _
                                 And m.ACCOUNTNO = acctNo _
                                 And m.CUSTTYPE = lvsCustType
                              Select m).ToList

            If DeletedQuery.Count > 0 Then
                For i As Integer = 0 To DeletedQuery.Count - 1
                    KYCEntity.MCUST_DEPOSIT_OBJ.DeleteObject(DeletedQuery(i))
                Next i
            End If
            ''''''''''''''''''''''
            For Each lvsRow_No As GridViewRow In Me.grdPAcc.Rows
                Dim newCust_Dept_Obj As New MCUST_DEPOSIT_OBJ

                ''''get values from Gridview '''''''

                mDO_Acctno = lvsRow_No.Cells(0).Text : If mDO_Acctno = "&nbsp;" Then mDO_Acctno = ""
                mDO_Objective = lvsRow_No.Cells(2).Text : If mDO_Objective = "&nbsp;" Then mDO_Objective = ""
                mDO_Remarks = lvsRow_No.Cells(3).Text : If mDO_Remarks = "&nbsp;" Then mDO_Remarks = ""
                If (CType(lvsRow_No.Cells(4).FindControl("chActivePAcc"), CheckBox).Checked = True) Then
                    chActive = "Y"
                Else
                    chActive = "N"
                End If

                ''''''''''''''''''''''''''''''''''''

                '''''''LINQ Start here '''''''''''''
                newCust_Dept_Obj.CUSTSEQ = key
                newCust_Dept_Obj.CUSTTYPE = Session("KYC")
                newCust_Dept_Obj.SEQ = lvsRow_No.RowIndex + 1
                newCust_Dept_Obj.ACCOUNTNO = mDO_Acctno.Trim
                newCust_Dept_Obj.OBJECTIVE = mDO_Objective.Trim
                newCust_Dept_Obj.REMARKS = mDO_Remarks.Trim
                newCust_Dept_Obj.ACTIVE = chActive
                newCust_Dept_Obj.CREATED_DATE = Now
                newCust_Dept_Obj.CREATED_BY = clsSessionMenuRight.getUserName
                KYCEntity.AddToMCUST_DEPOSIT_OBJ(newCust_Dept_Obj)

            Next
            KYCEntity.SaveChanges()

        Catch ex As Exception
            Throw ex

        End Try
    End Sub

    Protected Sub grdPAcc_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdPAcc.SelectedIndexChanged
        If indexGrdPAccBefore = grdPAcc.SelectedIndex Then
            grdPAcc.SelectedIndex = -1
        End If
    End Sub

    Protected Sub grdPAcc_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles grdPAcc.SelectedIndexChanging
        indexGrdPAccBefore = grdPAcc.SelectedIndex
    End Sub

    Private Sub clearPAcc()
        grdPAcc.SelectedIndex = -1
        ddPAcc.SelectedIndex = -1
        txtPAccOther.Text = ""
    End Sub

#End Region

#Region "แหล่งที่มาของเงินฝาก-ตั๋วเงิน"

    Protected Sub btAddPSource_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btAddPSource.Click
        Me.Add_To_Grid_PN()
    End Sub

    Sub Add_To_Grid_PN()
        Try
            If Trim(TxtAccNo.Text) = "" Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", "alert('ต้องเลือกเลขที่บัญชีก่อนทำการเพิ่มข้อมูล')", True)
                Exit Sub
            End If

            Dim maxSeq As Int32
            If ds.Tables("MCUST_DEPOSIT_SOURCE").Rows.Count = 0 Then
                maxSeq = 1
                If Not ds.Tables("MCUST_DEPOSIT_SOURCE").Columns.Contains("ACCOUNTNO") Then ds.Tables("MCUST_DEPOSIT_SOURCE").Columns.Add("ACCOUNTNO")
                If Not ds.Tables("MCUST_DEPOSIT_SOURCE").Columns.Contains("SOURCE_OF_M") Then ds.Tables("MCUST_DEPOSIT_SOURCE").Columns.Add("SOURCE_OF_M")
                If Not ds.Tables("MCUST_DEPOSIT_SOURCE").Columns.Contains("REMARKS") Then ds.Tables("MCUST_DEPOSIT_SOURCE").Columns.Add("REMARKS")
                If Not ds.Tables("MCUST_DEPOSIT_SOURCE").Columns.Contains("ACTIVE") Then ds.Tables("MCUST_DEPOSIT_SOURCE").Columns.Add("ACTIVE")
                If Not ds.Tables("MCUST_DEPOSIT_SOURCE").Columns.Contains("SEQ") Then ds.Tables("MCUST_DEPOSIT_SOURCE").Columns.Add("SEQ")
            Else
                Dim maxRow() As DataRow
                Dim strQry As String = "SEQ = MAX(SEQ)"
                maxRow = ds.Tables("MCUST_DEPOSIT_SOURCE").Select(strQry)
                maxSeq = maxRow(0)("SEQ") + 1
            End If
            Dim dTable As DataRow = ds.Tables("MCUST_DEPOSIT_SOURCE").NewRow

            dTable("ACCOUNTNO") = TxtAccNo.Text
            dTable("SOURCE_OF_M") = ddPSource.SelectedItem
            dTable("REMARKS") = txtPSourceOther.Text
            dTable("SEQ") = maxSeq
            dTable("ACTIVE") = True
            ds.Tables("MCUST_DEPOSIT_SOURCE").Rows.Add(dTable)

            grdPSource.DataSource = ds.Tables("MCUST_DEPOSIT_SOURCE")
            grdPSource.DataBind()
            clearPSource()
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(Page, _
                                                            Page.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub btDelPSource_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btDelPSource.Click
        Me.Delete_GridRow_PSource()
    End Sub

    Sub Delete_GridRow_PSource()
        Try

            Dim index As Integer = grdPSource.SelectedIndex
            If index = -1 Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", "alert('ต้องเลือกแหล่งที่มาของเงินฝากที่ต้องการลบก่อน')", True)
                Exit Sub
            Else
                ds.Tables("MCUST_DEPOSIT_SOURCE").Rows(grdPSource.SelectedIndex).Delete()
            End If
            clearPSource()
            grdPSource.DataSource = ds.Tables("MCUST_DEPOSIT_SOURCE")
            grdPSource.DataBind()
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(Page, _
                                                            Page.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub chActivePSource_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim rowTable As Int32
        For rowTable = 0 To grdPSource.Rows.Count - 1
            With ds.Tables("MCUST_DEPOSIT_SOURCE")
                .Rows(rowTable).Item("ACTIVE") = CType(grdPSource.Rows(rowTable).FindControl("chActivePSource"), CheckBox).Checked
            End With
        Next
        grdPSource.DataSource = ds.Tables("MCUST_DEPOSIT_SOURCE")
        grdPSource.DataBind()
    End Sub

    Protected Sub Save_to_MCUST_SOURCE_PN(ByVal key As Integer, ByVal acctNo As String)

        Dim mDS_Acctno As String = ""
        Dim mDS_Source As String = ""
        Dim mDS_Remarks As String = ""
        Dim chActive As String = ""
        Dim lvsCustType As Integer = Session("KYC")
        '''''''''''''''''''''''
        If lvsCustType <> 3 Then Exit Sub
        Try

            Dim DeletedQuery = (From m In KYCEntity.MCUST_DEPOSIT_SOURCE
                               Where m.CUSTSEQ = key _
                                 And m.ACCOUNTNO = acctNo _
                                 And m.CUSTTYPE = lvsCustType
                              Select m).ToList

            If DeletedQuery.Count > 0 Then
                For i As Integer = 0 To DeletedQuery.Count - 1
                    KYCEntity.MCUST_DEPOSIT_SOURCE.DeleteObject(DeletedQuery(i))
                Next i
            End If
            ''''''''''''''''''''''
            For Each lvsRow_No As GridViewRow In Me.grdPSource.Rows
                Dim newCust_Dept_Source As New MCUST_DEPOSIT_SOURCE

                ''''get values from Gridview '''''''
                mDS_Acctno = lvsRow_No.Cells(0).Text : If mDS_Acctno = "&nbsp;" Then mDS_Acctno = ""
                mDS_Source = lvsRow_No.Cells(2).Text : If mDS_Source = "&nbsp;" Then mDS_Source = ""
                mDS_Remarks = lvsRow_No.Cells(3).Text : If mDS_Remarks = "&nbsp;" Then mDS_Remarks = ""
                If (CType(lvsRow_No.Cells(4).FindControl("chActivePSource"), CheckBox).Checked = True) Then
                    chActive = "Y"
                Else
                    chActive = "N"
                End If

                ''''''''''''''''''''''''''''''''''''

                '''''''LINQ Start here '''''''''''''
                newCust_Dept_Source.CUSTSEQ = key
                newCust_Dept_Source.CUSTTYPE = Session("KYC")
                newCust_Dept_Source.SEQ = lvsRow_No.RowIndex + 1
                newCust_Dept_Source.ACCOUNTNO = mDS_Acctno.Trim
                newCust_Dept_Source.SOURCE_OF_M = mDS_Source.Trim
                newCust_Dept_Source.REMARKS = mDS_Remarks.Trim
                newCust_Dept_Source.ACTIVE = chActive
                newCust_Dept_Source.CREATED_DATE = Now
                newCust_Dept_Source.CREATED_BY = clsSessionMenuRight.getUserName
                KYCEntity.AddToMCUST_DEPOSIT_SOURCE(newCust_Dept_Source)

            Next
            KYCEntity.SaveChanges()

        Catch ex As Exception
            Throw ex

        End Try
    End Sub

    Protected Sub grdPSource_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdPSource.SelectedIndexChanged
        If indexGrdPSourceBefore = grdPSource.SelectedIndex Then
            grdPSource.SelectedIndex = -1
        End If
    End Sub

    Protected Sub grdPSource_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles grdPSource.SelectedIndexChanging
        indexGrdPSourceBefore = grdPSource.SelectedIndex

    End Sub

    Private Sub clearPSource()
        grdPSource.SelectedIndex = -1
        ddPSource.SelectedIndex = -1
        txtPSourceOther.Text = ""
    End Sub

#End Region

    Sub Bind_Data_togrid_Product()

        '''' Connect Data to Grid '''''''''''
        Me.dtProduct.Columns.Add("PRODUCTNAME")
        Me.dtProduct.Columns.Add("PRODUCTCODE")
        Me.dtProduct.Columns.Add("RISK")
        Me.dtProduct.Columns.Add("ACTIVE")

        Dim lvsCustType As Integer = Session("KYC")

        Dim QryProduct = (From tbName In KYCEntity.MCUST_PRODUCT
                        Where tbName.CUSTSEQ = CustSeq
                       Select tbName.SEQ,
                              tbName.PRODUCTNAME,
                              tbName.PRODUCTCODE,
                              tbName.RISK,
                              ACTIVE = If(tbName.ACTIVE = "Y", True, False)
                     Order By SEQ)

        For Each mRow In QryProduct
            Me.dtProduct.Rows.Add(mRow.PRODUCTNAME,
                                 mRow.PRODUCTCODE,
                                 mRow.RISK,
                                 mRow.ACTIVE)

            Me.dtProduct.AcceptChanges()
        Next

        Me.grdPRODUCT.DataSource = Me.dtProduct
        Me.grdPRODUCT.DataBind()
        ''''Number of Rows ''''''''''''''''''''''''''''''''''
        Me.lblCount_Product.Text = Me.dtProduct.Rows.Count
        '''''''''''''''''''''''''''''''''''''''''''''''''''''
        Session("PopProduct") = Me.dtProduct

    End Sub

    Sub Bind_Data_togrdAddedService()

        '''' Connect Data to Grid '''''''''''
        Me.dtAddedService.Columns.Add("ADDEDSERVICENAME")
        Me.dtAddedService.Columns.Add("ADDEDSERVICECODE")
        Me.dtAddedService.Columns.Add("RISK")
        Me.dtAddedService.Columns.Add("ACTIVE")

        Dim lvsCustType As Integer = Session("KYC")

        Dim qryAddedService = (From tbName In KYCEntity.MCUST_ADDED_SERVICE
                        Where tbName.CUSTSEQ = CustSeq
                       Select tbName.SEQ,
                              tbName.ADDEDSERVICENAME,
                              tbName.ADDEDSERVICECODE,
                              tbName.RISK,
                              ACTIVE = If(tbName.ACTIVE = "Y", True, False)
                     Order By SEQ)

        For Each mRow In qryAddedService
            Me.dtAddedService.Rows.Add(mRow.ADDEDSERVICENAME,
                                 mRow.ADDEDSERVICECODE,
                                 mRow.RISK,
                                 mRow.ACTIVE)

            Me.dtAddedService.AcceptChanges()
        Next

        Me.grdAddedService.DataSource = Me.dtAddedService
        Me.grdAddedService.DataBind()
        ''''Number of Rows ''''''''''''''''''''''''''''''''''
        Me.lblCount_AddedService.Text = Me.dtAddedService.Rows.Count
        '''''''''''''''''''''''''''''''''''''''''''''''''''''
        Session("PopAddedService") = Me.dtAddedService

    End Sub

    Protected Sub chk_ACT_ADDEDSERVICE_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        ''''check Session ''''''''''''''''''''''
        If Not (Session("PopAddedService") Is Nothing) Then
            Me.dtAddedService = Session("PopAddedService")
        End If

        '''''' assign checkbox value to data table 
        For i = 0 To Me.grdAddedService.Rows.Count - 1
            Me.dtAddedService.Rows(i).Item("ACTIVE") = CType(Me.grdAddedService.Rows(i).Cells(3).FindControl("chk_ACT_ADDEDSERVICE"), CheckBox).Checked
        Next i

        Me.dtAddedService.AcceptChanges()
        Me.grdAddedService.DataSource = Me.dtAddedService
        Me.grdAddedService.DataBind()

        Session("PopAddedService") = Me.dtAddedService
    End Sub
    Protected Sub chk_ACT_PRODUCT_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        ''''check Session ''''''''''''''''''''''
        If Not (Session("PopProduct") Is Nothing) Then
            Me.dtProduct = Session("PopProduct")
        End If

        '''''' assign checkbox value to data table 
        For i = 0 To Me.grdPRODUCT.Rows.Count - 1
            Me.dtProduct.Rows(i).Item("ACTIVE") = CType(Me.grdPRODUCT.Rows(i).Cells(3).FindControl("chk_ACT_PRODUCT"), CheckBox).Checked
        Next i

        Me.dtProduct.AcceptChanges()
        Me.grdPRODUCT.DataSource = Me.dtProduct
        Me.grdPRODUCT.DataBind()

        Session("PopProduct") = Me.dtProduct
    End Sub

    Protected Sub btDelAcc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btDelAcc.Click
        Try


            Dim index As Integer = grdShowAccount.SelectedIndex
            If index = -1 Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", "alert('ต้องเลือกบัญชีที่ต้องการลบก่อน')", True)
                Exit Sub
            Else
                ds.Tables("RKYC").Rows(grdShowAccount.SelectedIndex).Delete()
                If Me.lblKYCSEQ.Text <> "-1" Then
                    Me.Delete_RKYC_AcctNo(CustSeq, Me.TxtAccNo.Text)
                End If

            End If

            grdShowAccount.DataSource = ds.Tables("RKYC")
            grdShowAccount.DataBind()
            Me.lblCount_Show_ACCT.Text = ds.Tables("RKYC").Rows.Count
            resetData()


            ''''ลบข้อมูลออกจากหน้าจอ''''''''''''
            If ds.Tables.Contains("MCUST_BO_ACCT") Then
                ds.Tables.Remove("MCUST_BO_ACCT")
            End If
            Me.grdBo.DataBind()
            Me.lblCount_grdBo.Text = "0"
            Me.chkBO_SELF.Checked = False : Me.chkBO_OTH.Enabled = False
            Me.chkBO_OTH.Checked = False

            ''''ลบข้อมูลออกจากหน้าจอ''''''''''''
            If ds.Tables.Contains("MCUST_DEPOSIT_OBJ") Then
                ds.Tables.Remove("MCUST_DEPOSIT_OBJ")
            End If
            Me.grdDAcc.DataBind()
            Me.grdLAcc.DataBind()
            Me.grdPAcc.DataBind()
            ''''Grid Row Count 
            Me.lblCount_grdDAcc.Text = 0
            Me.lblCount_grdLAcc.Text = 0
            Me.lblCount_grdPAcc.Text = 0
            ''''''''''''''''''''''''''''''''''''''''''''''
            If ds.Tables.Contains("MCUST_DEPOSIT_SOURCE") Then
                ds.Tables.Remove("MCUST_DEPOSIT_SOURCE")
            End If
            Me.grdDSource.DataBind()
            Me.grdPSource.DataBind()
            ''''Grid Row Count 
            Me.lblCount_grdDSource.Text = 0
            Me.lblCount_grdPSource.Text = 0
            ''''''''''''''''''''''''''''''''''''''''''''''

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(Page, _
                                                            Page.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try


    End Sub

    Private Sub Delete_RKYC_AcctNo(ByVal custSEQ As Integer, ByVal acctNo As String)

        Try

            Dim lvsQryforDel = (From Cust In KYCEntity.RKYC
                               Where Cust.CUSTSEQ = custSEQ _
                                 And Cust.ACCOUNTNO = acctNo
                              Select Cust).First()

            KYCEntity.RKYC.DeleteObject(lvsQryforDel)
            KYCEntity.SaveChanges()

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub chkBO_SELF_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkBO_SELF.CheckedChanged
        If Trim(TxtAccNo.Text) = "" Then
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", "alert('ต้องเลือกเลขที่บัญชีก่อน')", True)
            Exit Sub
        End If
        '''' Added on Dec 03, 2014 ''''''''''
        If Me.chkBO_SELF.Checked = True Then
            Me.btAddBo_SELF.Enabled = True
        Else
            Me.btAddBo_SELF.Enabled = False
        End If
        '''''''''''''''''''''''''''''''''''''

        Dim dTable() As DataRow
        Dim strQry As String = "AccountNO = '" & grdShowAccount.Rows(grdShowAccount.SelectedIndex).Cells(1).Text & "'"
        dTable = ds.Tables("RKYC").Select(strQry)
        dTable(0)("FLAG_SELF_BO") = chkBO_SELF.Checked
        grdShowAccount.DataSource = ds.Tables("RKYC")
        grdShowAccount.DataBind()
    End Sub

    Protected Sub btAddBo_SELF_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btAddBo_SELF.Click
        Dim maxSeq As Int32

        If ds.Tables("MCUST_BO_ACCT") Is Nothing Then
            Exit Sub
        ElseIf ds.Tables("MCUST_BO_ACCT").Rows.Count = 0 Then
            maxSeq = 1
            If Not ds.Tables("MCUST_BO_ACCT").Columns.Contains("ACCOUNTNO") Then ds.Tables("MCUST_BO_ACCT").Columns.Add("ACCOUNTNO")
            If Not ds.Tables("MCUST_BO_ACCT").Columns.Contains("THFIRSTNAME") Then ds.Tables("MCUST_BO_ACCT").Columns.Add("THFIRSTNAME")
            If Not ds.Tables("MCUST_BO_ACCT").Columns.Contains("THLASTNAME") Then ds.Tables("MCUST_BO_ACCT").Columns.Add("THLASTNAME")
            If Not ds.Tables("MCUST_BO_ACCT").Columns.Contains("UNIQUEID") Then ds.Tables("MCUST_BO_ACCT").Columns.Add("UNIQUEID")
            If Not ds.Tables("MCUST_BO_ACCT").Columns.Contains("ACC_OWN_RELATION") Then ds.Tables("MCUST_BO_ACCT").Columns.Add("ACC_OWN_RELATION")
            If Not ds.Tables("MCUST_BO_ACCT").Columns.Contains("FILTER_RESULT") Then ds.Tables("MCUST_BO_ACCT").Columns.Add("FILTER_RESULT")
            If Not ds.Tables("MCUST_BO_ACCT").Columns.Contains("RISK_LEVEL") Then ds.Tables("MCUST_BO_ACCT").Columns.Add("RISK_LEVEL")
            If Not ds.Tables("MCUST_BO_ACCT").Columns.Contains("ACTIVE") Then ds.Tables("MCUST_BO_ACCT").Columns.Add("ACTIVE")
            If Not ds.Tables("MCUST_BO_ACCT").Columns.Contains("CIF_NO") Then ds.Tables("MCUST_BO_ACCT").Columns.Add("CIF_NO")
            If Not ds.Tables("MCUST_BO_ACCT").Columns.Contains("PASSPORT_NO") Then ds.Tables("MCUST_BO_ACCT").Columns.Add("PASSPORT_NO")
            If Not ds.Tables("MCUST_BO_ACCT").Columns.Contains("THMIDNAME") Then ds.Tables("MCUST_BO_ACCT").Columns.Add("THMIDNAME")
            If Not ds.Tables("MCUST_BO_ACCT").Columns.Contains("SEQ") Then ds.Tables("MCUST_BO_ACCT").Columns.Add("SEQ")
        Else
            For i As Integer = 0 To ds.Tables("MCUST_BO_ACCT").Rows.Count - 1
                If ds.Tables("MCUST_BO_ACCT").Rows(i).Item("UNIQUEID").ToString = txtBizNo.Text Then
                    Exit Sub
                End If
            Next

            Dim maxRow() As DataRow
            Dim strQry As String = "SEQ = MAX(SEQ)"
            maxRow = ds.Tables("MCUST_BO_ACCT").Select(strQry)
            maxSeq = maxRow(0)("SEQ") + 1
        End If

        Dim mcbo As DataRow = ds.Tables("MCUST_BO_ACCT").NewRow()
        mcbo("ACCOUNTNO") = TxtAccNo.Text
        mcbo("THFIRSTNAME") = Me.txtNameTH.Text
        mcbo("THLASTNAME") = ""
        mcbo("UNIQUEID") = txtBizNo.Text
        mcbo("ACC_OWN_RELATION") = "เจ้าของ"
        mcbo("FILTER_RESULT") = Me.lblFILTER_RESULT_CUST.Text
        mcbo("RISK_LEVEL") = Me.lblRISK_LEVEL_CUST.Text
        mcbo("ACTIVE") = True
        mcbo("CIF_NO") = ""
        mcbo("PASSPORT_NO") = ""
        mcbo("THMIDNAME") = ""
        mcbo("SEQ") = maxSeq
        ds.Tables("MCUST_BO_ACCT").Rows.Add(mcbo)

        grdBo.DataSource = ds.Tables("MCUST_BO_ACCT")
        grdBo.DataBind()
        ''''Number of Rows '''''''''''''''''''''
        Me.lblCount_grdBo.Text = ds.Tables("MCUST_BO_ACCT").Rows.Count
        ''''''''''''''''''''''''''''''''''''''''
    End Sub

    Protected Sub btDelBo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btDelBo.Click
        Try

            Dim index As Integer = grdBo.SelectedIndex
            If index = -1 Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", "alert('ต้องเลือก BO ที่ต้องการลบก่อน')", True)
                Exit Sub
            Else
                ds.Tables("MCUST_BO_ACCT").Rows(grdBo.SelectedIndex).Delete()
            End If

            grdBo.DataSource = ds.Tables("MCUST_BO_ACCT")
            grdBo.DataBind()
            Me.lblCount_grdBo.Text = ds.Tables("MCUST_BO_ACCT").Rows.Count
            ClearFieldBo()

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(Page, _
                                                Page.GetType(), _
                                                "clientScript", _
                                                "alert('" + ex.Message.Replace("'", "") + "')", _
                                                True)
        End Try
    End Sub

End Class
